Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

awk

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.

like image 509
pogibas Avatar asked Jun 08 '13 17:06

pogibas


People also ask

How do I use substr in awk?

substr(str, start, l) This function returns the substring of string str, starting at index start of length l. If length is omitted, the suffix of str starting at index start is returned.

What does tilde mean in awk?

In addition to normal arithmetic and logical operators, AWK expressions include the tilde operator, ~ , which matches a regular expression against a string.

How do I print an entire line in awk?

To print a blank line, use print "" , where "" is the empty string. To print a fixed piece of text, use a string constant, such as "Don't Panic" , as one item. If you forget to use the double-quote characters, your text is taken as an awk expression, and you will probably get an error.


2 Answers

awk '$3 ~ /snow/ { print }' dummy_file 
like image 97
Ahmed Masud Avatar answered Sep 22 '22 20:09

Ahmed Masud


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
like image 40
Thunderbeef Avatar answered Sep 21 '22 20:09

Thunderbeef