-   Programming (
-   -   action on unmatched pattern - awk (

sopier 12-13-2011 10:52 AM

action on unmatched pattern - awk
say I have this file:


sopier is learning awk
If my pattern doesn't match, I want to print "dont match" else print $0. Here is my command which is return none.


awk '/sopir/ {if ($0 ~ /^$/) print "dont match"; else print $0}' file1

Cedrik 12-13-2011 11:12 AM

A way to do it:

awk '!/sopir/{print "dont match";next} {print}' file1

sopier 12-13-2011 11:24 AM

Thank you

sopier 12-13-2011 12:50 PM

what if there are no matched pattern, I want to insert a new line in the files instead of print, let's say i have this file"


sopier is learning awk
sopier is a newbie


awk '!/sopir/{this action inserting a new line}' file1 > temp
Thank you...

Cedrik 12-13-2011 01:36 PM

You mean insert a newline after each non matching lines ?

If so:

awk '!/sopir/{print $0,"\n";next} {print}' file1 > temp

sopier 12-13-2011 08:31 PM

Thank you.... Case closed..

All times are GMT -5. The time now is 07:34 PM.