#Basic AWK Part II: Slightly more advanced AWK #Written by E Heppenheimer, Spring 2018 #Using AWK to extract entire rows based on value of certain column #For example, in a tab separated file, Column 1 is the chromosome ID and you want to pull out just chromosome 14 #I like to be in the habit of specifying the field separator (-F) #But, this is technically not necessary unless the field separator is something other than white space (i.e. NOT a space or a tab) $ awk -F "\t" '$1 == 14 ' infile > chromosome14_only.txt #or for some reason you only want to keep lines where column 8 is equal to 26 $ awk '$8 == 26 ' infile > outfile #and so on... #It is also possible to specify a range of values #e.g. only keep rows where column 12 is greater than or equal to 0.19 $ awk ' $12 >=0.19 ' infile > outfile #or less than 0.25 $ awk ' $12 < 0.25 ' infile > outfile #This also works for strings (that is, column X does not have to be a number) $ awk -F "\t" '$1 == "chr4"' infile > outfile