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.
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.
In addition to normal arithmetic and logical operators, AWK expressions include the tilde operator, ~ , which matches a regular expression against a string.
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.
awk '$3 ~ /snow/ { print }' dummy_file
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With