0

I have a text file named 0test.txt with the following snippet of code in it. (It has several http and https url links but these lines single it out)

player.setup({
  file: 'http://website.com/site/myStream/playlist.m3u8?wmsAuthSign=c2VydmVyXs',
  width: "100%",
  aspectratio: "16:9",

});

I can run a sed command:

cat /tmp/0test.txt | sed -n -e "/^ *file: */ { s/^ *file: *'//; s/', *$//p}"

And get back the correct URL link:

http://website.com/site/myStream/playlist.m3u8?wmsAuthSign=c2VydmVyXs

But when the file 0test.txt looks like this:

player.setup({
          file: 
'http://website.com/site/myStream/playlist.m3u8?wmsAuthSign=c2VydmVyX3RpbWU9MTIvNC8yMDE5IDk6MTY6NTggUE0maGFzaF92YWx1ZT16RXpiUWowQ0V4TDJER3ExQnFZTEd3PT0mdmFsaWRtaW51dGVzPTM2MCZzdHJtX2xlbj0w',
          width: "100%",
          aspectratio: "16:9",
            aspectratio: "16:9"

    });

I do not get anything back. How can I fix this?

6
  • Is the URL string really quoted with single quotes? AFAIK, that's not allowed in a JSON document. Also, that first JSON document ends with a dangling comma. The general gist of a solution may be to remove the code bits with sed and then ask jq for the value associated with the file key, but since the JSON is malformed, that won't work.
    – Kusalananda
    Dec 4 '19 at 22:25
  • Is there a reason for not using something along the line of grep -E -o 'http.*[[:alnum:]]' here?
    – FelixJN
    Dec 4 '19 at 22:50
  • @Kusalananda Yes it's really quoted with single quotes.
    – Rick T
    Dec 4 '19 at 22:57
  • @Fiximan I'm open to using grep or awk but the key is the text file: There's dozen of other http and https urls but only one file:
    – Rick T
    Dec 4 '19 at 22:57
  • 1
    Can you exclude that another URL (directly) follows a line with file: <URL>? If so you could use multiple instances of grep: grep -A 1 'file:' <testfile> | grep -oE 'http.*' | sed "s:',$::"
    – FelixJN
    Dec 4 '19 at 23:07
1

For this specific case, grep might be more useful. In a first step the buzzword line: is searched and and the following line added to the output. Then the URL itself is selected thereof using grep and finally sed for removing the trailing single quote and comma:

grep -A 1 'file:' <infile(s)> | grep -oE 'http.*' | sed "s:',$::" 
0

In your regular expression, you are allowing an arbitrary number of space-characters after the colon ("file: *"). You might want to expand that to a general whitespace character ("\s") so that line breaks and tabs are treated "like spaces".

0

A quick fix.

cat /tmp/0test.txt|sed ':a;N;$!ba;s/file:\s*\n/file: /g;'

the line will transform the file in the way, that each line ending with "file:" (with or without extra spaces at the end) will merge with a line going after it.

In this way, the text

player.setup({
          file: 
'http://website.com/site/myStream/playlist.m3u8?wmsAuthSign=c2VydmVyX3RpbWU9MTIvNC8yMDE5IDk6MTY6NTggUE0maGFzaF92YWx1ZT16RXpiUWowQ0V4TDJER3ExQnFZTEd3PT0mdmFsaWRtaW51dGVzPTM2MCZzdHJtX2xlbj0w',
          width: "100%",

is going to transform to

player.setup({
          file: 'http://website.com/site/myStream/playlist.m3u8?wmsAuthSign=c2VydmVyX3RpbWU9MTIvNC8yMDE5IDk6MTY6NTggUE0maGFzaF92YWx1ZT16RXpiUWowQ0V4TDJER3ExQnFZTEd3PT0mdmFsaWRtaW51dGVzPTM2MCZzdHJtX2xlbj0w',
          width: "100%",

and now you can use your old approach.

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.