Friday, 15 July 2016

Using the linux grep, sort, and awk commands

The linux grep, sort and awk commands are really useful, here are some of the nice features: (I haven't added much here yet, but plan to add as I go along..)

grep examples
% grep -f  file1 file2
Finds patterns specified in file1, in file2, eg. file1 could have a list of words, and this would search for them in file2.

Find lines of a file that have some alphabetical character:
% grep -E '[A-Za-z]' file1

sort examples
Sort using a tab as delimiter:
% sort -k15,15nr -t$'\t' file1

awk examples
Filter a tab-deliminted file to just take lines in which column 17 has value >=50:
% awk -F"\t" '($17 >= 50)'  file1

Filter a tab-delminited file to just take lines in which column 2 has value '727':
% awk -F"\t" '$2 == 727' file1

Filter a tab-deliminted file to just take lines in which column 2 has string 'snow':
% awk -F"\t" '($2=="snow") {print}' file1

tr examples
Get rid of the character " from all lines:
% cat file1 | tr -d '"'


No comments: