LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-13-2011, 09:52 AM   #1
sopier
Member
 
Registered: Dec 2011
Location: Jogja, Indonesia
Distribution: Ubuntu
Posts: 33

Rep: Reputation: Disabled
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
 
Old 12-13-2011, 10:12 AM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
A way to do it:
Code:
awk '!/sopir/{print "dont match";next} {print}' file1
 
Old 12-13-2011, 10:24 AM   #3
sopier
Member
 
Registered: Dec 2011
Location: Jogja, Indonesia
Distribution: Ubuntu
Posts: 33

Original Poster
Rep: Reputation: Disabled
Thank you
 
Old 12-13-2011, 11:50 AM   #4
sopier
Member
 
Registered: Dec 2011
Location: Jogja, Indonesia
Distribution: Ubuntu
Posts: 33

Original Poster
Rep: Reputation: Disabled
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...
 
Old 12-13-2011, 12:36 PM   #5
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
You mean insert a newline after each non matching lines ?

If so:
Code:
awk '!/sopir/{print $0,"\n";next} {print}' file1 > temp
 
Old 12-13-2011, 07:31 PM   #6
sopier
Member
 
Registered: Dec 2011
Location: Jogja, Indonesia
Distribution: Ubuntu
Posts: 33

Original Poster
Rep: Reputation: Disabled
Thank you.... Case closed..
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help to replace a pattern with sed or awk rramesh1 Linux - Newbie 2 11-22-2011 12:06 AM
[SOLVED] awk pattern matching philipz *BSD 1 05-05-2010 02:21 PM
How to get the pattern using sed or awk? ahpin Programming 3 08-02-2007 03:16 AM
boot problem. after "INIT: version 2.85 booting: awk: fatal: Unmatched ( dobreman Fedora 2 05-27-2006 11:12 AM
xlsfonts: pattern "*" unmatched Zitan Linux - General 1 08-07-2003 10:56 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration