LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   script help (https://www.linuxquestions.org/questions/linux-newbie-8/script-help-404437/)

manicman 01-17-2006 08:28 AM

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

homey 01-17-2006 08:47 AM

Hi,

Sed will do it...

sed 's/test>\(.*\)<info/\1/g' file.txt

nx5000 01-17-2006 08:49 AM

Code:

sed 's/test>\(.*\)<info/\1/' <file>

timmeke 01-17-2006 09:54 AM

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.

manicman 01-17-2006 02:14 PM

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

homey 01-17-2006 03:55 PM

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

manicman 01-17-2006 05:00 PM

yay it works thanks for your help and i think im slowley starting to understand this command as well

homey 01-17-2006 05:10 PM

You're welcome! :)


All times are GMT -5. The time now is 08:42 PM.