LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 10-01-2011, 06:31 PM   #1
wolverene13
Member
 
Registered: May 2010
Location: Matiland, FL
Distribution: Debian Squeeze
Posts: 49

Rep: Reputation: 0
Awk to search for a string and remove the line above if it does not contain a word


Hi,

I have been writing a script for work for some time now and almost have it completed, except for one last step. I use Linux at home, so I am used to using GNU grep to find lines above and below the search string (using -A and -B), but we use Solaris at work and unfortunately it doesn't have GNU grep. I have basically had to learn awk and sed to get around this. What I really need to accomplish is printing one particular string (we'll call it "string1") or another string ("string2") and the line above string2 if it is found in the file. I have been using:

Code:
awk '/(^string1)|(^ string2)/{print x; print};{x=$0}/'
...to print the line above the two strings I am searching for so far, which has worked, but unfortunately it also prints the line above string1. I have tried all sorts of different awk commands and played around with the logic, but either it doesn't work and errors out, or I don't get the output I need.

Example file ("NODE" would be "string1" and "description" would be "string2" if using the code I pasted above):

NODE_NAME_1
PORT 1
description 123 blah
PORT 2
PORT 3
PORT 4
description 456 blah blah
PORT 5
NODE_NAME_2
PORT 1
PORT 2
description flibbity jibbity
PORT 3
description 789 blah blah blah
PORT 4
description 101112 blah blah
PORT 5
NODE_NAME_3
PORT 1
PORT 2
PORT 3
PORT 4
PORT 5

I basically need all NODE_NAME lines, plus any PORT lines, but only if they are followed by "description." I want the output to look like this:

NODE_NAME_1
PORT 1
description 123 blah
PORT 4
description 456 blah blah
NODE_NAME_2
PORT 2
description flibbity jibbity
PORT 3
description 789 blah blah blah
PORT 4
description 101112 blah blah
NODE_NAME_3

However, it comes out like this currently:

NODE_NAME_1
PORT 1
description 123 blah
PORT 4
description 456 blah blah
PORT 5
NODE_NAME_2
PORT 2
description flibbity jibbity
PORT 3
description 789 blah blah blah
PORT 4
description 101112 blah blah
PORT 5
NODE_NAME_3

Obviously, I've caused awk to think I want the line above NODE_NAME too. Any ideas? Thanks in advance!
 
Old 10-01-2011, 06:51 PM   #2
crts
Senior Member
 
Registered: Jan 2010
Posts: 1,604

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Hi,

a small adjustment to your awk solutiont should take care of the issue:
Code:
awk '/(^NODE)/{print};/(^description)/{print x; print};{x=$0}'
 
Old 10-01-2011, 07:05 PM   #3
wolverene13
Member
 
Registered: May 2010
Location: Matiland, FL
Distribution: Debian Squeeze
Posts: 49

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by crts View Post
Hi,

a small adjustment to your awk solutiont should take care of the issue:
Code:
awk '/(^NODE)/{print};/(^description)/{print x; print};{x=$0}'
Yes! That worked! *Insert idiotic happy dance here*
Thank you so much!! Now I can enjoy my weekend!
 
Old 10-01-2011, 07:13 PM   #4
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 1,771

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by crts View Post
Hi,

a small adjustment to your awk solutiont should take care of the issue:
Code:
awk '/(^NODE)/{print};/(^description)/{print x; print};{x=$0}'
This is a good fix but the code prints an extra line when the first line matches string2 or when a line matches string1 and the next line matches string2. If either case is possible, then you can do:

Code:
awk '/^NODE/{print; x=""} /^description/ {if (x != "") print x; print} {x=$0}'
 
  


Reply

Tags
awk, line, print


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
awk print word on each line on 3 column mad_penguin Programming 9 09-22-2011 06:59 AM
How can awk search a string without using regular expression? 915086731 Programming 8 09-07-2011 11:07 PM
Search word and delete only the word and the line using Sed command kbmukesh Linux - Newbie 4 06-28-2011 07:35 AM
remove color after word search in vi editor dylan912 Linux - Newbie 1 12-10-2004 05:15 AM


All times are GMT -5. The time now is 04:06 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration