LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   action on unmatched pattern - awk (http://www.linuxquestions.org/questions/programming-9/action-on-unmatched-pattern-awk-918516/)

sopier 12-13-2011 09:52 AM

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

Code:

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.

Code:

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

Cedrik 12-13-2011 10:12 AM

A way to do it:
Code:

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

sopier 12-13-2011 10:24 AM

Thank you

sopier 12-13-2011 11:50 AM

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"

Code:

sopier is learning awk
sopier is a newbie

Code:

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

Cedrik 12-13-2011 12:36 PM

You mean insert a newline after each non matching lines ?

If so:
Code:

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

sopier 12-13-2011 07:31 PM

Thank you.... Case closed..


All times are GMT -5. The time now is 07:50 AM.