LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 03-04-2007, 03:01 PM   #1
rahulbgl
LQ Newbie
 
Registered: Mar 2007
Posts: 1

Rep: Reputation: 0
Post Need ideas on how to grep for pattern using awk


Hi All,
I have a text like this
002000 2010 4302
002020 1234 5689
002040 1234 5689
*
002060 1034 5689
002080 1234 5689
002100 1234 5689
002120 1234 5689
*
002140 1334 5689
.
I am trying to write an awk script where in i need to get the text line next to the line printed by star namely
002000 2010 4302 (First line always)
002060 1034 5689
002140 1334 5689
.
Any ideas?
Thanks,
Rah
 
Old 03-04-2007, 03:37 PM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Hi.

This should do it:
Code:
awk 'NR<2 {print}; /^\*$/ {getline; print}'
The 'NR<2 {print}' prints the first line. The '/^\*$/ {getline; print}' finds lines consisting only of a '*', then prints the next line.

Dave
 
Old 03-04-2007, 09:17 PM   #3
7stud
LQ Newbie
 
Registered: Feb 2007
Posts: 22

Rep: Reputation: 15
Here's my attempt:

awk.scr:
Code:
BEGIN { prevLine="" } #not necessary since awk will automatically initialize to ""

#MAIN
{
        if(prevLine=="*")
                print $0;
        else if(prevLine=="") #prevLine is only blank for 1st line(see BEGIN)
                print $0;

        prevLine=$0;
}
command:
Code:
 awk -f awk.scr data.txt
data.txt:
Quote:
002000 2010 4302
002020 1234 5689
002040 1234 5689
*
002060 1034 5689
002080 1234 5689
002100 1234 5689
002120 1234 5689
*
002140 1334 5689
output:
Quote:
002000 2010 4302
002060 1034 5689
002140 1334 5689

Last edited by 7stud; 03-05-2007 at 03:14 AM.
 
Old 03-05-2007, 11:29 AM   #4
7stud
LQ Newbie
 
Registered: Feb 2007
Posts: 22

Rep: Reputation: 15
...and with sed:
Code:
sed -n -f sed.scr data.txt
The -n option suppresses output, which an be overridden with the p function when necessary.

sed.scr:
Code:
1p            prints first line
/*/{          searches for a line with a * on it and if found executes the commands in the braces  
        n;    outputs pattern space if the -n option isn't set, then replaces pattern space with next line
        p;    prints the pattern space
}

Last edited by 7stud; 03-05-2007 at 08:50 PM.
 
Old 03-05-2007, 04:07 PM   #5
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Ah, the joys of Unix - 80 ways to do everything.
 
  


Reply


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
find | grep <pattern> not working duvalr Linux - Software 4 09-17-2006 05:07 AM
awk print lines that doesn't have a pattern huynguye Programming 5 05-04-2006 12:08 PM
pattern matching question - grep cbriscoejr Programming 1 02-09-2006 09:30 PM
using grep when the pattern contains a ! farmerjoe Programming 9 03-16-2005 12:04 AM
Grep pattern first line of a file ericcarlson Linux - Newbie 11 07-20-2004 11:51 AM


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

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration