Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum. |
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
06-12-2007, 10:22 AM
|
#1
|
Member
Registered: Oct 2004
Posts: 84
Rep:
|
How can I grep text from file?
I would like to grep the following text from a file called file.txt:
Code:
<p class="maintxt">
- You should include as much detail as possible in your message,
including exact error messages (where applicable) and what
you have done so far. The more detail you include the more we
can help.</p>
If I do:
Code:
cat file.txt | grep "<p class=\"maintxt\">" > file2.txt
I only get one line with <p class="maintxt"> in file2.txt
How can I grep so that I get all the text from <p class="maintxt"> to </p>?
Thanks!
_______________________
@ngelot
|
|
|
06-12-2007, 10:43 AM
|
#2
|
Senior Member
Registered: Sep 2005
Location: Out
Posts: 3,307
Rep:
|
Maybe this:
Code:
sed -n -e '/<p class="maintxt">/,/<\/p>/p'
|
|
|
06-12-2007, 10:51 AM
|
#3
|
Senior Member
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847
Rep:
|
Or
Code:
grep -A4 "<p class=\"maintxt\">" file.txt > file2.txt
Assuming that text you want is always going to be exactly 4 lines after the match.
edit: @ngelot, UUOC (uselss use of cat) - grep will take a file name as an input argument, therefore no need to cat the file first.
Last edited by pwc101; 06-12-2007 at 10:54 AM.
|
|
|
06-13-2007, 05:10 AM
|
#4
|
Member
Registered: Oct 2004
Posts: 84
Original Poster
Rep:
|
Thanks!
I found out that awk does the job:
Code:
awk '/<p class=\"maintxt\">/, /<\/p>/' file.txt > file2.txt
Liked the UUOC - thats the essens of computing : do things easier!
_______________________
@ngelot
|
|
|
06-13-2007, 05:33 AM
|
#5
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,274
|
Quote:
Originally Posted by UUOC
If it's only one file, concatenating it with nothing at all is a waste of time
|
Nope.
Not if you want to read it and "cat" it to STDOUT ...
|
|
|
06-13-2007, 05:38 AM
|
#6
|
Senior Member
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847
Rep:
|
Quote:
Originally Posted by syg00
Nope.
Not if you want to read it and "cat" it to STDOUT ...
|
Point taken, but there are also tools specifically for that (less and more to name but two). Such is the *nix way!
edit: On occasion, I've caught myself doing something like this:
Code:
cat file.txt | less
d'oh!
|
|
|
06-13-2007, 05:44 AM
|
#7
|
LQ Newbie
Registered: Feb 2005
Location: Vejle, DK
Distribution: Kubuntu, Debian
Posts: 4
Rep:
|
What about this way?
Code:
grep "<p class=\"maintxt\">.*</p>" file.txt > file2.txt
(Note: I have not tested this, only theory!)
Also, don't pipe cat to grep. Just grep directly from the file!
|
|
|
All times are GMT -5. The time now is 12:48 PM.
|
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
|
|