Extract a string between 2 ref string from a file

Hi,

May i ask if someone share some command for extracting a string between 2 ref string in a txt file

My objective: i had a file with multiple lines and wants only to extract the string "watch?v=IbkAXOmEHpY" or "watch?v=<11 random character>", when i used "grep 'watch?=*' i got a results per line

<unneeded char>href="/watch?v=IbkAXOmEHpY&feature=plcp<unneeded char>

, and used cut for finally.

What i would like is not having using grep and cut instead sed or similar built in command for extracting directly the "watch?v=IbkAXOmEHpY" without using grep first.

Maybe sed the first ref would be <href="> and end ref <&amp>

Im searching now and found some sed and perl but no luck at all..

Hope someone can share time..

Thanks in advance

perl -nle '/href="\/([^&]+)/;print $1' file

If your grep knows "-o", you could try something like:

grep -o 'watch?v=[^&;]*'
1 Like

@Scrutinizer: works fine thanks, i would like to ask also how to exclude the empty 11 character, the results include the "watch?v=<empty>" i would like to exclude this one,

@bartus11: thanks also, but it will only works if all lines contain the

"works if grep first and all lines contain the refference, the file contains also some other character or some lines thats doesn't have

it seems i print also other line with other field set, by the way the file is a source web page.

By the way thanks for a quick response.

You could run the output through: | grep -v '<empty>'