Search code examples
linuxunixawkstring-comparisoncsh

C Shell Substring Compare


My data looks like the following:

1xxxxxx file_name1
0xxxxxx file_name2
0xxxxxx file_name3
0xxxxxx file_name4
1xxxxxx file_name5
0xxxxxx file_name6

I would like to print only the file names with the first column starting with a '1'.

That is,

awk '{if($1 contains '1') then print $2}' file.txt

However, I haven't a clue how to achieve the comparison syntax

Perhaps a wildcard method might be possible?

awk '{if($1 == "1"*) then print $2}' file.txt

Solution

  • Try this:

    awk '$1~/^1/{print $2}' file.txt
    

    You check the first column ($1) using a match operator (~) against pattern which looks for line starting with 1 (/^1/). If it is true, the action of printing second column takes place.