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,


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


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

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

cut -d \" -f 2 < filename
  • I agree, this is exactly what cut was designed for. – Ryan Foley Feb 16 '14 at 15:06
sed 's/.*"\(http.*\)" .*/\1/' filename

This is more portable, since some of the other answers depend on href being the first element

grep -o href.*\" file.txt | cut -d \" -f 2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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