LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Grep -p for Linux, Trying to grep a paragraph. (https://www.linuxquestions.org/questions/linux-newbie-8/grep-p-for-linux-trying-to-grep-a-paragraph-821544/)

ohijames 07-22-2010 01:10 PM

Grep -p for Linux, Trying to grep a paragraph.
 
$> cat file.txt
default:
expires = 0
SYSTEM = "compat"
logintimes =
pwdwarntime = 14
account_locked = false
loginretries = 3

root:
admin = true
login = true
SYSTEM = "compat"


daemon:
admin = true

$>
Hi There,

I have the following information in a text file called file.txt and i am trying to do a grep -p "root:" file.txt to get the following output only

root:
admin = true
login = true
SYSTEM = "compat"

this works fine in AIX but not on Linux.

i found the following code on another thread but i don't understand the code or how it works or it it is going to work for me.

awk 'BEGIN{RS=ORS="\n\n";FS=OFS="\n"}NF>=4' file

Any help will be great and appreciated.

Tinkster 07-22-2010 01:38 PM

awk, like grep, in its default invocation, operates on
lines (records, separated by newlines), and fields (by
default separated by whitespace other than newlines).

What the awk snippet above does is to re-define records
(RS=record separator, ORS = output RS) and fields (FS=
field separator, OFS=output FS) to be the following
RS = "\n\n" = two subsequent new lines (in other words: an empty line)
FS = "\n"

So, an empty line defines a boundary between two records
(which is what you referred to as a paragraph).

Now, with the BEGIN statement awk has its behaviour
for all records and fields defined as you need them,
with NF >=4 it will print any record that has more than
4 fields (i.e., a paragraph with 4 or more lines).


HIH


Cheers,
Tink

ncsuapex 07-22-2010 01:49 PM

One way to do it with grep IF you know exactly how many lines you want to print is.

Quote:

cat file.txt | grep root -A 3

This greps for root and prints the 3 lines after

ohijames 07-22-2010 01:49 PM

@ Tinkster, My issue with the code is >4. I'm running my script on different text files that have different values. some have lines less than for and some have lines greater than 4. is there anyway I can use sed, grep or awk to capture a paragraph just like the grep -p works in AIX regardless of the size of the paragraph.

Tinkster 07-22-2010 02:00 PM

Quote:

Originally Posted by ohijames (Post 4042221)
@ Tinkster, My issue with the code is >4. I'm running my script on different text files that have different values. some have lines less than for and some have lines greater than 4. is there anyway I can use sed, grep or awk to capture a paragraph just like the grep -p works in AIX regardless of the size of the paragraph.

Of course; just use a regex instead of the NF>4
Code:

awk 'BEGIN{RS=ORS="\n\n";FS=OFS="\n"}/root:/' file

Cheers,
Tink

ohijames 07-22-2010 02:09 PM

@Tinkster. Thank you very much. Cheers


All times are GMT -5. The time now is 10:27 PM.