LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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


Reply
  Search this Thread
Old 06-12-2007, 10:22 AM   #1
@ngelot
Member
 
Registered: Oct 2004
Posts: 84

Rep: Reputation: 15
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
 
Old 06-12-2007, 10:43 AM   #2
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 57
Maybe this:

Code:
sed -n -e '/<p class="maintxt">/,/<\/p>/p'
 
Old 06-12-2007, 10:51 AM   #3
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
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.
 
Old 06-13-2007, 05:10 AM   #4
@ngelot
Member
 
Registered: Oct 2004
Posts: 84

Original Poster
Rep: Reputation: 15
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
 
Old 06-13-2007, 05:33 AM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,274

Rep: Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165Reputation: 4165
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 ...
 
Old 06-13-2007, 05:38 AM   #6
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
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!
 
Old 06-13-2007, 05:44 AM   #7
tfault
LQ Newbie
 
Registered: Feb 2005
Location: Vejle, DK
Distribution: Kubuntu, Debian
Posts: 4

Rep: Reputation: 0
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!
 
  


Reply


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
Grep certain text from logfile grimse Programming 17 09-07-2006 08:06 PM
Grep text man_linux Linux - General 3 09-02-2006 01:06 AM
How to search and replace a text using grep DediPlace Linux - General 2 05-29-2005 07:47 PM
Using grep, etc to modify a text file dangerousdave Linux - Newbie 7 02-23-2005 03:49 PM
remove text with grep craigdolson Linux - General 6 04-21-2004 04:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:48 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration