Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a file which is as below.

<a href="http://firstlink.com" title="title1">
<a href="http://secondlink.com" title="title2">
<a href="http://thirdlink.com" title="title3">
<a href="http://fourthlink.com" title="title4">

I am trying to extract only the URLs from the above file. I am using the below command.

grep -o '\".*\"' new.txt

However, the above command gives me the output as,

"http://firstlink.com" title="title1">
"http://secondlink.com" title="title2">
"http://thirdlink.com" title="title3">
"http://foruthlink.com" title="title4">

I am trying to extract only the URLs without the "". So, my expected output is,

http://firstlink.com
http://secondlink.com
http://thirdlink.com
http://fourthlink.com

How should I change the grep command? Or is it possible to do it in perl, awk or sed command?

share|improve this question
add comment

3 Answers

up vote 4 down vote accepted

You could use awk.

awk -F\" '{print $2}' filename

would produce the desired output.

Using sed:

sed 's/[^"]*"\([^"]*\).*/\1/' filename

Using grep:

grep -oP '[^"]*"\K[^"]*' filename
share|improve this answer
    
Thanks. awk command worked. –  Ramesh Feb 11 at 18:44
    
Not able to accept the answer now. Will accept it after 7 minutes. –  Ramesh Feb 11 at 18:45
add comment

regexp, stream editors and interpreters are overkill here.
Use the old good cut :

cut -d \" -f 2 < filename
share|improve this answer
    
I agree, this is exactly what cut was designed for. –  Fizzle Feb 16 at 15:06
add comment
sed 's/.*"\(http.*\)" .*/\1/' filename
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.