awk partly string match (if column/word partly matches)

Awk

Awk Problem Overview


My dummy file looks like this:

C1    C2    C3    
1     a     snow   
2     b     snowman 
snow     c     sowman

I want to get line if there is string snow in $3. I can do this like this:

awk '($3=="snow" || $3=="snowman") {print}' dummy_file

But there should be more simpler way.

Awk Solutions


Solution 1 - Awk

awk '$3 ~ /snow/ { print }' dummy_file 

Solution 2 - Awk

Also possible by looking for substring with index() function:

awk '(index($3, "snow") != 0) {print}' dummy_file

Shorter version:

awk 'index($3, "snow")' dummy_file

Solution 3 - Awk

Maybe this will help

http://www.math.utah.edu/docs/info/gawk_5.html

awk '$3 ~ /snow|snowman/' dummy_file

Solution 4 - Awk

Print lines where the third field is either snow or snowman only:

awk '$3~/^snow(man)?$/' file

Solution 5 - Awk

GNU sed
sed '/\s*\(\S\+\s\+\)\{2\}\bsnow\(man\)\?\b/!d' file

Input:

C1    C2    C3
1 a snow
2 b snowman snow c sowman snow snow snowmanx

..output:

1     a     snow
2     b     snowman

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpogibasView Question on Stackoverflow
Solution 1 - AwkAhmed MasudView Answer on Stackoverflow
Solution 2 - AwkThunderbeefView Answer on Stackoverflow
Solution 3 - AwkAhmed Al HafoudhView Answer on Stackoverflow
Solution 4 - AwkChris SeymourView Answer on Stackoverflow
Solution 5 - AwkEndoroView Answer on Stackoverflow