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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
07-22-2010, 01:10 PM
|
#1
|
|
LQ Newbie
Registered: Jun 2010
Posts: 10
Rep:
|
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.
|
|
|
|
07-22-2010, 01:38 PM
|
#2
|
|
Moderator
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,902
|
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
|
|
|
|
07-22-2010, 01:49 PM
|
#3
|
|
Member
Registered: Dec 2004
Location: Raleigh, NC
Distribution: CentOS 2.6.18-53.1.4.el5
Posts: 770
Rep:
|
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
|
|
|
|
07-22-2010, 01:49 PM
|
#4
|
|
LQ Newbie
Registered: Jun 2010
Posts: 10
Original Poster
Rep:
|
@ 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.
|
|
|
|
07-22-2010, 02:00 PM
|
#5
|
|
Moderator
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,902
|
Quote:
Originally Posted by ohijames
@ 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.
|
07-22-2010, 02:09 PM
|
#6
|
|
LQ Newbie
Registered: Jun 2010
Posts: 10
Original Poster
Rep:
|
@Tinkster. Thank you very much. Cheers
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:51 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|