- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: perl or awk command to print next line after p...
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
Forums
Discussions
Discussions
Discussions
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2010 07:04 AM
03-17-2010 07:04 AM
Re: perl or awk command to print next line after pattern in txt file ??
> 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.
This would be true if your environmental variable weren't defined. While I accept what you say, Verify what Perl sees by adding the warnings pragma:
# # perl -wne'/$ENV{ERROR}/i...($.+1) and print' file
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2010 07:32 AM
03-17-2010 07:32 AM
Re: perl or awk command to print next line after pattern in txt file ??
As JRF asked, have you exported ERROR?
env | grep ERROR
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2010 08:05 AM
03-17-2010 08:05 AM
Re: perl or awk command to print next line after pattern in txt file ??
In fact, perhaps you meant to export the variable for the duration of the command line but added a semicolon instead:
# ERROR="Mar 16" perl -wne'/$ENV{ERROR}/i...($.+1) and print' file
...works, but this does not:
# ERROR="Mar 16";perl -wne'/$ENV{ERROR}/i...($.+1) and print' file
Too, in the second case:
# echo $ERROR
...would look like the variable were defined (and maybe exported), *but* as Dennis also suggested, 'env|grep ERROR' would not return anything.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2010 09:25 AM
03-17-2010 09:25 AM
Re: perl or awk command to print next line after pattern in txt file ??
"Brain lock error" on my behalf. I must have not exported the variable. All good and veery, very thx to help me jog my rusty perl brain.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2010 11:46 AM
03-17-2010 11:46 AM
Re: perl or awk command to print next line after pattern in txt file ??
grep -e error -n file |\
while read data
do
line=${data%%:*}
eval head "-$(( $line + 1 )) foo" | tail -1
done
Yes, this assumes that error is not on the last line of the file...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-17-2010 12:10 PM
03-17-2010 12:10 PM
Re: perl or awk command to print next line after pattern in txt file ??
As a side note, in your original post you do
$ cat file | perl -e'......'
which is one process too many.
Both
$ perl -e'....' file
and
$ perl -e'....' < file
would be more effective. Sorry I want to say it, but it is a pet-peeve of mine. Don't waste process slots.
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-19-2010 09:25 AM
03-19-2010 09:25 AM
Re: perl or awk command to print next line after pattern in txt file ??
Create sed file as below and assume the pattern is root
/root/
{
n
p
}
Then run the sed command as
sed -n -f sed.file
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-20-2010 02:50 AM
03-20-2010 02:50 AM
Re: perl or awk command to print next line after pattern in txt file ??
It appears you aren't printing the line you match but only the next one.