Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find a string in a string using awk

Tags:

string

awk

here is column 6 in a file:

ttttttttttt
tttttttttt
ttttttttt
tttttttattt
tttttttttt
ttttttttttt

how can I use awk to print out lines that include "a"

like image 695
AWE Avatar asked Dec 12 '11 21:12

AWE


4 Answers

If you only want to search the sixth column, use:

awk '$6 ~ /a/' file

If you want the whole line, any of these should work:

awk /a/ file

grep a file

sed '/^[^a]*$/d' file
like image 99
Kevin Avatar answered Sep 18 '22 12:09

Kevin


If you wish to print only those lines in which 6th column contains a then this would work -

awk '$6~/a/' file

like image 41
jaypal singh Avatar answered Sep 19 '22 12:09

jaypal singh


if it is an exact match (which yours is not) you're looking for:

$6 == "a"

http://www.pement.org/awk/awk1line.txt is an excellent resource

awk can also tell you where the pattern is in the column:

awk '{++line_num}{ if ( match($6,"a")) { print "found a at position",RSTART, " line " ,line_num}  }' file

though this example will only show the first "a" in column 6; a for loop would be needed to show all instances (I think)

like image 36
Henry Barber Avatar answered Sep 19 '22 12:09

Henry Barber


You could try

gawk '{ if ( $1 ~ /a/ ) { print $1  } }' filename
like image 23
souser Avatar answered Sep 19 '22 12:09

souser