Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 9 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 '14 at 18:44
Not able to accept the answer now. Will accept it after 7 minutes. – Ramesh Feb 11 '14 at 18:45

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. – Ryan Foley Feb 16 '14 at 15:06
sed 's/.*"\(http.*\)" .*/\1/' filename
share|improve this answer

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
share|improve this answer

Your Answer


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.