- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- perl or awk command to print next line after patte...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-16-2010 12:30 PM
тАО03-16-2010 12:30 PM
How do I grep/print the next line after a pattern ?
With perl, I only go this far but needs to know what comes in between double quotes after perl -e.
Or , I am open to other awk/sed suggestions.
Thx
cat file | perl -e " "; while (<>) {print if /error/ }"
Solved! Go to Solution.
- Tags:
- grep
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-16-2010 12:57 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-16-2010 01:02 PM
тАО03-16-2010 01:02 PM
Re: perl or awk command to print next line after pattern in txt file ??
Bingo !! Thx
That is what I need. i got a equivalent awk command but I like PERl.
10 pointer answer. Now, I need to figure out where you store the lines by cracking this oreilly perl book.
awk '/error/{print;for(i=0;i<1;i++){getline;print}}' file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-16-2010 06:31 PM
тАО03-16-2010 06:31 PM
Re: perl or awk command to print next line after pattern in txt file ??
grep -A1 error file ...
For sed:
sed -n '/error/ { p; n; p; }' file ...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-16-2010 08:21 PM
тАО03-16-2010 08:21 PM
Re: perl or awk command to print next line after pattern in txt file ??
I get exactly what I want. But please help me out here.
When i substitute error for a variable $ERROR and set $ERROR=error, I dont get same result.
I am having same issues with JRF solution. I have put double quotes, single quote but dont get same thing.
Thx
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-16-2010 11:24 PM
тАО03-16-2010 11:24 PM
Re: perl or awk command to print next line after pattern in txt file ??
Because everything is in '' which says don't evaluate $ variables. I typically use single quotes just so I won't get incorrect evaluation. For your three cases:
sed -n "/$ERROR/ { p; n; p; }" file ...
perl -nle "if (/$ERROR/i && !eof) {chomp(\$line=<>); print \"\$_\n\$line\"}" file
Note the quoting of the other "$" and double quotes.
You could also stutter quotes:
perl -nle 'if (/'"$ERROR"'/i && !eof) {chomp($line=<>); print "$_\n$line"}' file
For awk, don't play that game, use -v to pass them:
awk -v string="$ERROR" '$0 ~ string {print; for(i=0;i<1;i++){getline; print}}' file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-17-2010 12:22 AM
тАО03-17-2010 12:22 AM
Re: perl or awk command to print next line after pattern in txt file ??
Just because I couldn't resist to show a shorter version. Also safe at eof.
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-17-2010 12:40 AM
тАО03-17-2010 12:40 AM
Re: perl or awk command to print next line after pattern in txt file ??
$ perl -ne'/$ENV{ERROR}/i...($.+1) and print' file
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-17-2010 04:27 AM
тАО03-17-2010 04:27 AM
Re: perl or awk command to print next line after pattern in txt file ??
For clarity, I should have eliminated the needless addition ('-l') and subtraction ('chomp') of newlines in my original suggestion, reducing it to:
# perl -ne 'if (/error/i && !eof) {$line=<>;print $_,$line}' file
However, Merijn's solution is by far better golf and much more Perl DWIM :-)
With 'awk' as Dennis noted, rather than fiddling around escaping the shell special characters, one generally passes environmental variables with the '-v' construct.
In Perl, leveraging the 'ENV' hash pays dividends as Merijn stated. You can do this too in 'awk'. As a simple example, compare:
# export MYVAR=ERROR
# echo "ERROR\nerror\nError"|awk '{if (tolower($0)~tolower(ENVIRON["MYVAR"])) {print}}'
# export MYVAR=ERROR
# echo "ERROR\nerror\nError"|perl -ne '/$ENV{MYVAR}/i and print'
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-17-2010 06:37 AM
тАО03-17-2010 06:37 AM
Re: perl or awk command to print next line after pattern in txt file ??
Your modification of JRF perl works beautifully. sed does too.
Much thanks to Procura and JRF again.
Procura, As JRF agreed, your shorter version is better and I would like to use that.
However, I get different results, when I am trying to grep on pattern , for instance . "Mar 16" from a file when I used the ENV variable as mentioned below.(Please see below). If i hard code "Mar 16", I get right results.
What am I doing wroing ???
BIG THANKS to ALL OF YOU . I 've learned a lot.
# perl -ne'/Mar 16/i...($.+1) and print' file
Mar 16 11:48:06 2009 : kevi137 : TTY=pts/3 ; PWD=/export/home/kevi137 ;
USER=root ; COMMAND=/usr/local/bin/su_tibco
Mar 16 15:48:44 2010 : chri119 : command not allowed ; TTY=pts/1 ;
PWD=/export/home/chri119 ; USER=root ; COMMAND=/usr/local/bin/su_tibcoadm /app/tibco/pmcrefresh/test
Mar 16 15:51:56 2010 : chri119 : TTY=pts/7 ; PWD=/export/home/chri119 ;
USER=root ; COMMAND=/app/tibco/pmcrefresh/test
Mar 16 15:53:45 2010 : chri119 : TTY=pts/7 ; PWD=/app/tibco/pmcrefresh ;
USER=root ; COMMAND=/app/tibco/pmcrefresh/test.sh
# echo $ERROR
Mar 16
# perl -ne'/$ENV{ERROR}/i...($.+1) and print' file
Sep 21 08:09:14 2007 : wesle15 : user NOT authorized on host ; TTY=pts/1 ;
PWD=/export/home/wesle15 ; USER=root ; COMMAND=su_tibco
Sep 21 08:29:43 2007 : wesle15 : 1 incorrect password attempt ; TTY=pts/2 ;
PWD=/export/home/wesle15 ; USER=root ; COMMAND=su_tibco
Sep 21 08:53:20 2007 : wesle15 : user NOT authorized on host ; TTY=pts/1 ;
PWD=/export/home/wesle15 ; USER=root ; COMMAND=su_tibco
Sep 21 08:57:01 2007 : wesle15 : command not allowed ; TTY=pts/8 ;
PWD=/export/home/wesle15 ; USER=root ; COMMAND=su_tibco
Sep 21 08:59:26 2007 : nichol5 : command not allowed ; TTY=pts/1 ; PWD=/ ;
USER=root ; COMMAND=su_tibco
Sep 21 09:00:25 2007 : nichol5 : TTY=pts/1 ; PWD=/ ; USER=root ; COMMAND=/usr/lo
cal/bin/su_tibco
Sep 21 09:01:01 2007 : wesle15 : TTY=pts/8 ; PWD=/export/home/wesle15 ;
USER=root ; COMMAND=/usr/local/bin/su_tibco
Sep 21 09:36:09 2007 : harin01 : TTY=pts/9 ; PWD=/export/home/harin01 ;
USER=root ; COMMAND=/usr/local/bin/su_tibco