LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 01-17-2006, 09:28 AM   #1
manicman
Member
 
Registered: Oct 2005
Location: England (Southampton)
Distribution: Ubuntu 6.06 , Gentoo
Posts: 62

Rep: Reputation: 15
script help


hello im trying to output a large amount of data from a text file and im not sure how to do this i want to output the text between two words for example if the words where "test>" and "<info" and i had this file
Code:
test>test1<info
test>test2<info
test>test3<info
i would want it to output
Code:
test1
test2
test3
i belive sed is the right command to use for this but i cant figure it out could do with some help thanks
 
Old 01-17-2006, 09:47 AM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
Hi,

Sed will do it...

sed 's/test>\(.*\)<info/\1/g' file.txt
 
Old 01-17-2006, 09:49 AM   #3
nx5000
Senior Member
 
Registered: Sep 2005
Location: Out
Posts: 3,307

Rep: Reputation: 55
Code:
sed 's/test>\(.*\)<info/\1/' <file>
 
Old 01-17-2006, 10:54 AM   #4
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
If you actually have a delimiter like ">" or "<", you could also use "cut":

In your example:
cut -d'>' -f2 file |cut -d'<' -f1

should do the trick too.
 
Old 01-17-2006, 03:14 PM   #5
manicman
Member
 
Registered: Oct 2005
Location: England (Southampton)
Distribution: Ubuntu 6.06 , Gentoo
Posts: 62

Original Poster
Rep: Reputation: 15
thanks a lot that works all apart from one problem it seems out output the text on the line after <info for example if i have
Code:
test>test1<info
test>test2<info(more info)
test>test3<info
it will out output
Code:
test1
test2(more info)
test3
is there a way to stop this ive worked out how to use
sed -n 's/test>\(.*\)<info/\1/gp' <file>
to filter out other lines but i cant seem to work this last part out
 
Old 01-17-2006, 04:55 PM   #6
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
I went a step more and put some text in front also....
Code:
test>test1<info
here is some test>test2<info(more info)
test>test3<info
So, I used .* to indicate one or more characters before test> and after <info
Code:
sed 's/.*test>\(.*\)<info.*/\1/g' file.txt
 
Old 01-17-2006, 06:00 PM   #7
manicman
Member
 
Registered: Oct 2005
Location: England (Southampton)
Distribution: Ubuntu 6.06 , Gentoo
Posts: 62

Original Poster
Rep: Reputation: 15
yay it works thanks for your help and i think im slowley starting to understand this command as well
 
Old 01-17-2006, 06:10 PM   #8
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
You're welcome!
 
  


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
Iptables (with masq) troubleshooting, very simple script attached script and logs. xinu Linux - Networking 13 11-01-2007 05:19 AM
python cgi script and premature end of script headers Neruocomp Programming 1 07-28-2005 12:43 PM
creating shell script that executes as root regardless of who runs the script? m3kgt Linux - General 13 06-04-2004 11:23 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 01:20 AM
linux 9 and java script error - premature end of script header sibil Linux - Newbie 0 01-06-2004 05:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 11:43 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration