Print the line only if the line exists in a specific column

Need help with awk (or sed) command to select a row only if the value / String exists in a specific column.

Example:

I only want to return a row if the word "true" exists in column 5, do not return a row if "true" does not exist in column 5

input:

john,jacob,jingle,schmidt,true
jason,jack,john,true,false
oscar,meir,true,weiner,false
tiffany,amber,false,theissen,true
jack,john,sally,true,true

      

Desired output:

john,jacob,jingle,schmidt,true
tiffany,amber,false,theissen,true
jack,john,sally,true,true

      

I know how to pull back a line that has a word and doesn't have a word like this:

awk '/true/ && !/false/'

      

but that won't work and I know how to delete a word only if it's in a specific column

awk 'BEGIN{FS=OFS=","} {sub(/true */, "", $5)} 1'

      

but I don't know how to combine the two.

My best guess:

awk 'BEGIN{FS=OFS=","} {"/true/",$5}

      

worst case scenario .... :)

awk 'BEGIN{FS=OFS=","} {sub(/true */, "Unmatchablestring", $5)} 1' | awk '/Unmatchablestring/'

      

+3


source to share


2 answers


You can use:



awk -F, '$5 == "true"' file

      

+4


source


Your question was a bit vague as to whether you want to check if column 5 contains a literal value true

or if it should be an exact match. If you want to check if a column contains 5 true

, than use a match operator ~

like:

awk -F, '$5 ~ /true/' file

      



Otherwise follow anubhava's answer .

+4


source







All Articles