LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 07-22-2010, 01:10 PM   #1
ohijames
LQ Newbie
 
Registered: Jun 2010
Posts: 10

Rep: Reputation: 0
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.
 
Old 07-22-2010, 01:38 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
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
 
Old 07-22-2010, 01:49 PM   #3
ncsuapex
Member
 
Registered: Dec 2004
Location: Raleigh, NC
Distribution: CentOS 2.6.18-53.1.4.el5
Posts: 770

Rep: Reputation: 42
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
 
Old 07-22-2010, 01:49 PM   #4
ohijames
LQ Newbie
 
Registered: Jun 2010
Posts: 10

Original Poster
Rep: Reputation: 0
@ 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.
 
Old 07-22-2010, 02:00 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
Quote:
Originally Posted by ohijames View Post
@ 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
 
1 members found this post helpful.
Old 07-22-2010, 02:09 PM   #6
ohijames
LQ Newbie
 
Registered: Jun 2010
Posts: 10

Original Poster
Rep: Reputation: 0
@Tinkster. Thank you very much. Cheers
 
  


Reply

Tags
awk, grep, sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 10:31 PM
grep and print paragraph B-Boy Programming 2 08-11-2008 02:30 AM
how to grep multiple filters with grep LinuxLover Linux - Enterprise 1 10-18-2007 07:12 AM
Paragraph-option to grep TheSpork Linux - General 2 03-17-2006 01:45 AM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 11:38 AM


All times are GMT -5. The time now is 08:07 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration