- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: 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
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
тАО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.