1752290 Members
4169 Online
108786 Solutions
New Discussion юеВ

Re: grep help

 
SOLVED
Go to solution
George Chechakunnil
Frequent Advisor

grep help

Hi

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




I am like a small boy picking up pebbles in god's vast shore of knowledge --- Sir Issac Newton
10 REPLIES 10
Pete Randall
Outstanding Contributor

Re: grep help

George,

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
James R. Ferguson
Acclaimed Contributor

Re: grep help

Hi :

You could do :

# perl -e '$/="";while (<>) {print if /DEFAULT/}' file

or:

# awk 'BEGIN{RS=""};/DEFAULT/ {print}' file

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: grep help

>JRF: # perl -e '$/="";while (<>) {print if /DEFAULT/}' file
># 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.
James R. Ferguson
Acclaimed Contributor

Re: grep help

Hi:

> 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...
George Chechakunnil
Frequent Advisor

Re: grep help

Thanks for the Help but i am not getting the output as i want

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
I am like a small boy picking up pebbles in god's vast shore of knowledge --- Sir Issac Newton
Dennis Handly
Acclaimed Contributor
Solution

Re: grep help

>The attached is the input file. they are in paragraphs each of 4 lines each.

Your 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.
George Chechakunnil
Frequent Advisor

Re: grep help

Terrific

Thanks to all..

I am like a small boy picking up pebbles in god's vast shore of knowledge --- Sir Issac Newton
Tingli
Esteemed Contributor

Re: grep help

Although this post is too late, but I cannot help it.
sed -n -e ''/DEFAULT/'!{H;x;s/^.*\n\(.*\n.*\n.*\)$/\1/;x;}' -e ''/DEFAULT/'{H;x;p;}' File_Name
Hein van den Heuvel
Honored Contributor

Re: grep help

Also too late, also redundant, but maybe helpful to someone some day as it is more 'open ended'.

$ 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.