Print portion line in SED


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print portion line in SED
# 1  
Old 03-19-2009
Print portion line in SED

Hi,

This is more a theoretical question, because I usually solved that with perl or even java, but I would like to know if it exists an easy way to do it with SED.

Using regular expresions it's very easy to select an portion line. Does it exist an easy way for printing those portions in SED?

For example, let's suppose that in an HTML file I want to print all links. Selecting the links it's very easy. Just for keeping the things simple, let's suppose the HTML is a well formed one

href=['"]\([^'"]*\)['"]

so \1 holds the link. Now my question, does it exist an easy way in sed for printing all the links?

So, if I have

XXXX href='link1' ZZZ href='link2' XXXX

I want to have

link1
link2


The only way that I know for doing that in sed is deleting everything except the links, but I'm sure it must exist an easier way to do it.

Thanks a lot

Isi
# 2  
Old 03-19-2009
Code:
sed 's/<a/\n&/g' FILE |
 sed -n "/href/ s/.*href=['\"]\([^'\"]*\)['\"].*/\1/gp"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed - Print Edited Line Along With Original Line

Hi, I have an input file like this line1 line2 line3 hello unix how are you This is what I am expecting my output to be line1 line2 #line3 hello unix how are you line3 hello (3 Replies)
Discussion started by: jacobs.smith
3 Replies

2. Shell Programming and Scripting

sed script - print the line number along with the line

Hi, I want to print the line number with the pattern of the line on a same line using multi-patterns in sed. But i don't know how to do it. For example, I have a file abc def ghi I want to print 1 abc 2 def 3 ghi I know how to write it one line code, but i don't know how to put... (11 Replies)
Discussion started by: ntpntp
11 Replies

3. UNIX for Dummies Questions & Answers

use variable for sed print line

I'm using the sed command to pull a line from a file and place it into another file: sed -n '1p' students >tmp this pulls the first line, I want to change the 1 to a variable which will be a counter to go through the rest of the lines but can't get it. I've tried: sed -n '$cp students... (3 Replies)
Discussion started by: atchleykl
3 Replies

4. Shell Programming and Scripting

sed - print old line and new changed line

hello, i have a file "TEST" and want to change the digit(s) after "=" . but i also want to print the old entry with a comment (for information). i want to use "sed", is it possible ? file: TEST = 10 # comment default value: 0 sed , with "p" , i can print the old entry, but i want to... (2 Replies)
Discussion started by: bora99
2 Replies

5. Shell Programming and Scripting

sed: print out to first blank line

I am trying to use wget and sed to extract a text based weather forecast for the Greater Victoria area only, this is one paragraph of many in a web page. I have tried /^$/ but that does not seem to work. So far I get mostly what I want with this: wget -qO -... (2 Replies)
Discussion started by: lagagnon
2 Replies

6. Shell Programming and Scripting

Print previous, current and next line using sed

Hi, how can i print the previous, current and next line using sed? current line is the matching line. The following prints all lines containing 'Failure' and also the immediate next line cat $file | sed -n -e '/Failure/{N;p;}' Now, i also want to print the previous line too. Thanks,... (8 Replies)
Discussion started by: ysrinu
8 Replies

7. UNIX for Dummies Questions & Answers

Print a portion of file

Hi, I have a little problem. I am having a file with pattern like : asdf;ffgg;dfjfj;djdfjf;nnjj;djd;ssj; I just want to print the portion from last ";" upto the immediate previous ";". There are several ";" in my line. Please help me out... Thnx in advance (8 Replies)
Discussion started by: vanand420
8 Replies

8. Shell Programming and Scripting

Print a portion of a line

Hi, I am facing a little problem... I have a line like this : asdcvashfasashXXXXxxxzxcadd:sdcashjqdasdsmgdkdaxdsnd; I want to print just a portion of line i.e starting from left 5 characters from ":" and upto ";" i.e. in this case it would be "xcadd:sdcashjqdasdsmgdkdaxdsnd;" The length of... (2 Replies)
Discussion started by: vanand420
2 Replies

9. UNIX for Dummies Questions & Answers

erasing portion of line with sed (only once)

hi, I'm trying to use sed to erase everything, up to the first parenthesis. for example: input: blah blah blah (aldj) test (dafs) test test. output: (aldj) test (dafs) test test. how would i do this? I was fooling around with the parenthesis, and i only got it to apply on all parenthesis.... (1 Reply)
Discussion started by: gammaman
1 Replies

10. UNIX for Dummies Questions & Answers

erasing portion of line with sed

hi, I'm trying to use sed to erase everything, and including the ending parenthesis. For example: input: blah blah blah (12355)this is what i want. output: this is what i want. how would i do this? i found an example online that does the opposite: sed \"s|test.*||g\" file1 > file2"; ... (5 Replies)
Discussion started by: gammaman
5 Replies
Login or Register to Ask a Question