- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- grep help
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
07-28-2009 02:39 AM
07-28-2009 02:39 AM
i have a file with series of entries with 4 lines each in a group
How can i search for a pattern and print all the 4 lines
checking bbb on xny
The Password Aging policy is==> DISABLED
The Account Life time ==> NEVER EXPIRES
Number of unsuccesful logins Allowed ==> INFINITE
checking qwe on qwqw
The Password Aging policy is==> DISABLED
The Account Life time ==> NEVER EXPIRES
Number of unsuccesful logins Allowed ==> INFINITE
checking eqw on inqw
The Password Aging policy is==> DISABLED
The Account Life time ==> NEVER EXPIRES
Number of unsuccesful logins Allowed ==> DEFAULT
checking eqw on inqw
The Password Aging policy is==> DISABLED
The Account Life time ==> NEVER EXPIRES
Number of unsuccesful logins Allowed ==> INFINITE
for the one that has DEFAULT i want to print out the 4 lines above that
checking eqw on inqw
The Password Aging policy is==> DISABLED
The Account Life time ==> NEVER EXPIRES
Number of unsuccesful logins Allowed ==> DEFAULT
how can i do this?
Thanks in advance
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2009 02:52 AM
07-28-2009 02:52 AM
Re: grep help
From "Handy One-Liners for Sed" (attached):
# print paragraph if it contains AAA (blank lines separate paragraphs)
# HHsed v1.5 must insert a 'G;' after 'x;' in the next 3 scripts below
sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;'
Pete
Pete
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2009 03:37 AM
07-28-2009 03:37 AM
Re: grep help
You could do :
# perl -e '$/="";while (<>) {print if /DEFAULT/}' file
or:
# awk 'BEGIN{RS=""};/DEFAULT/ {print}' file
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2009 09:03 AM
07-28-2009 09:03 AM
Re: grep help
># awk 'BEGIN{RS=""};/DEFAULT/ {print}' file
I'm not sure how these print out the previous 3 lines and the line with DEFAULT?
I'm assuming the files have zillions of lines and possibly a few of these 4 line blocks.
Of course here GNU grep would work with -B3.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2009 09:15 AM
07-28-2009 09:15 AM
Re: grep help
> Dennis: I'm not sure how these print out the previous 3 lines and the line with DEFAULT?
They do _IF_ as the post suggests that the input file is composed of paragraphs with a blank line as the stanza seperator.
Of course, that's my assumption and it would be obviously better if the OP had _attached_ the input.
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2009 08:47 PM
07-28-2009 08:47 PM
Re: grep help
The attached is the input file.. they in paragraphs each of 4 lines each.
Now i want the output to have all the paragraphs that have the word DEFAULT in it.
Desired output
checking kcrpw on inuap700
The Password Aging policy is ==> DEFAULT
The Account Life time ==> NEVER EXPIRES
Number of unsuccesful logins Allowed ==>DEFAULT(6)
checking kcrpw on inuap705
The Password Aging policy is ==> DEFAULT
The Account Life time ==> NEVER EXPIRES
Number of unsuccesful logins Allowed ==>DEFAULT(6)
checking glprd on inuap706
The Password Aging policy is==> DISABLED
The Account Life time ==> NEVER EXPIRES
Number of unsuccesful logins Allowed ==>DEFAULT(6)
etc etc
Thanks a lot in advance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-28-2009 10:23 PM
07-28-2009 10:23 PM
SolutionYour input file doesn't have empty lines as separators, they have one space.
You will have to change what was given to:
sed -e '/...*/{H;$!d;}' -e 'x;/DEFAULT/!d' file
sed -e 's/^ *$//' file | awk '
BEGIN { RS=""}
/DEFAULT/ {print}'
Or explicit programming for what's there:
awk '
/^checking / {
getline l2; getline l3; getline l4
if (l2 ~ /DEFAULT/ || l3 ~ /DEFAULT/ || l4 ~ /DEFAULT/) {
print $0; print l2; print l3; print l4
}
}' file
>JRF: input file is composed of paragraphs with a blank line as the separator.
I found the standard where it describes RS="". It sure isn't on the man page.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-29-2009 01:28 AM
07-29-2009 01:28 AM
Re: grep help
Thanks to all..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-31-2009 10:51 AM
07-31-2009 10:51 AM
Re: grep help
sed -n -e ''/DEFAULT/'!{H;x;s/^.*\n\(.*\n.*\n.*\)$/\1/;x;}' -e ''/DEFAULT/'{H;x;p;}' File_Name
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-31-2009 12:00 PM
07-31-2009 12:00 PM
Re: grep help
$ perl -ne 'if (/\S/) { push @p,$_ } else {print @p if grep /DEFAULT/,@p; @p=("\n")}' x.txt
This looks for (arbitrary) paragraph markers, here a line with only whitespace.
IF a line with non=whitespace is found, it is pushed onto a paragraph array p.
ELSE the array p that was build up is grepped for a target (DEFAULT) and the whole array printed on match. The array is then reset to a single new-line for the next go around.
btw... if there might not be a final paragraph marker, then you may want to turn it arouns some to catch a hit on the last paragraph:
perl -ne '{ push @p,$_ } if (/^\s*$/ or eof) {print @p if grep /DEFAULT/,@p; @p=()}' a.txt
fwiw,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-31-2009 01:09 PM
07-31-2009 01:09 PM
Re: grep help
I just noticed this thread bubble to the top again.
> Dennis: Your input file doesn't have empty lines as separators, they have one space.
Ahh...now I see the reason for the confusion and the failure of my original suggestion to yield the output that George wanted [read on].
> Dennis: I found the standard where it describes RS="". It sure isn't on the man page.
Ahh, yes, again!
Based on the original post (before the attachment of text), it appeared to me that blank lines consisted solely of newline characters and these separated the stanzas or paragraphs. My suggest was based upon that assumption.
In Perl, when that is the case, if you set the record separator ('$/') to a null string, one or more blank lines constitute a record terminator. Unfortunately, a "blanK line can consist only of a newline character without hidden spaces or tabs. Perhaps more unfortunately, Perl doesn't allow a pattern in place of a simple string for the record separator.
In the GNU 'awk' documentation, it is noted that an empty string for the value of 'RS' signals that records are separated by one or more blank lines. Dennis is correct; this _isn't_ described on the HP-UX manpages. It works, though.
Of my two original code snippets, I should have slightly modified the 'awk' variation to give back at least one blank line. The Perl snippet could be better written for the way the Forum blends whitespace too:
# perl -e '$/=qq();while (<>) {print if /DEFAULT/}' file
or:
# awk 'BEGIN{RS=""};/DEFAULT/ {print $0, "\n"}' file
Finally, to filter your raw data so that it is suitable to being piped to either of the above snippets, you could do (for example):
# perl -pe 's/^\s+$/\n/' file | perl -e 'BEGIN{$/=qq()};while (<>) {print if /DEFAULT/}'
Regards!
...JRF...