12

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?

4 Answers 4

16

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
2
12

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

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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .