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. |
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 |
One way to do it with grep IF you know exactly how many lines you want to print is.
Quote:
This greps for root and prints the 3 lines after |
@ 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.
|
Quote:
Code:
awk 'BEGIN{RS=ORS="\n\n";FS=OFS="\n"}/root:/' file Cheers, Tink |
@Tinkster. Thank you very much. Cheers
|
All times are GMT -5. The time now is 10:27 PM. |