new to this forum. I am trying to pull off a weird script, but I am been stuck for some days now.

I have two text files.

Input.txt Playlist.txt

Input.txt Contains a number that is generated randomly every 4 hours.


Playlist.txt Contains M3u links.

#EXTINF:-1 tvg-id="" tvg-name="CHAN 1" tvg-logo="" group-title="FTA",CHAN 1

EXTINF:-1 tvg-id="" tvg-name="CHAN " tvg-logo="" group-title="FTA",CHAN 2

So I want to find and replace all the ocurrences of 010707 and replace them with the number inside the Input.txt file in this case 083107 then save a copy of the Playlist.txt file, and every 4 hours replace that number again with the one inside the Input.txt file and so on.

I already have put together a very ugly but working script that uses grep and wget to download some files of the internet and generate that Input.txt file.

Thank you for your time.


Every day I download a M3uPlaylist file "Playlist.m3u" from some site, lets call it example.com.

But, the page "example.com" generates a new user every 4 hours and I need to replace it in the m3u file manually using find (Old user number) and replace (new user number).

/live/**010707**/11/11594.ts to /live/**083107**/11/11594.ts

This is what I came up with:

I point wget to example.com and download only the part of the website containing the user.

wget -qO- 19X.71.5X.252 | grep -oP 'User:.*' > User.txt

User: 083107       Password: 11

Then I use grep to generate a "cleaner" file.

grep 'User' User.txt | grep -o '[0-9]*' > Numbers.txt

083107 11 2

After that I use the "head" command to make a file containing the user name only.

head -1 Numbers.txt > input.txt


What i want to acomplish is to automatically update the Playlist.txt (or M3u) file with the new user number example.com is generating every 4 hours. Using the input.txt file for that.

This is the part of example.com source code that contains the user.

<!--You need to visit this site every 4 hours in order to get a new user-->
        <div id="domainSearch" class="domain-search--section bg--overlay-color bg--overlay-opacity-95 pd--80-0 text-center" data-bg-img="img/domain-search-img/bg.jpg">
            <div class="container">
                <!-- Domain Search Title Start -->
                <div class="domain-search--title">
                     <h2 class="h3">Whatsapp: &nbsp;&nbsp; +52 </h3>
                    <h2 class="h2">User: 083107      &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  Password: 11</h2>
  • 1
    It's probably easier to create a template file in which the number is represented by some unambiguous placeholder, and the generate a fresh file from the template each time. Otherwise you're going to need to record the current value and construct a new replacement command each time. – steeldriver Jun 7 '18 at 17:11
  • 2
    Please edit and provide the script that creates Input.txt as well as what’s done with Playlist.txt in the end – there may be a much easier way to do the whole thing. What is the XY problem? – dessert Jun 7 '18 at 18:10
  • 1
    is "010707" the previous value of Input.txt? – glenn jackman Jun 7 '18 at 18:25
  • 1
    @glennjackman Yes it is, it changes every 4 hours. – Orlando Muñoz Acevedo Jun 7 '18 at 18:41

I'd do this:

# extract the userid from the current playlist
prev_user=$(grep -Po '(?<=live/).+(?=/11/11594.ts)' Playlist.txt | head -1)

# find the current userid
current_user=$(wget -qO- 19X.71.5X.252 | grep -oP 'User:\s*\K\S+')

# update the playlist
sed_body=$(printf 's/\<%s\>/%s/g' "$prev_user" "$current_user")
sed -i "$sed_body" Playlist.txt

That grep regex should just find the non-space characters after "User:" (i.e. the userid). This way you don't need the other temp files.

I also use the \< and \> word boundary markers for the sed regex so you match the userid as a whole word.


Updated version

Considering the fact that "010707" is the previous content of "Input.txt" file, I would change the script a bit.

while true
   NEW_INPUT=$(cat Input.txt)
   sed -i.backup "s/$PREVIOUS_INPUT/$NEW_INPUT/g" Playlist.txt
   sleep 14400

Run it with <name-of-the-script>.sh 010707.

And if You would append instead of overwrite: head -1 Numbers.txt >> Input.txt, it all would be even easier.

while true
   sed -i.backup "s/$(tail -2 Input.txt | head -1)/$(tail -1 Input.txt)/g" Playlist.txt
   sleep 14400

Original answer

sed is what You need: https://www.maketecheasier.com/what-is-sed/ .

while true
   sed "s/010707/$(cat Input.txt)/g" Playlist.txt > NewPlaylist.txt
   sleep 14400

should work.

You could also edit Playlist.txt "in place" with -i parameter. I would suggest using -i.backup in this case though, to store a previous version of the file as Playlist.txt.backup.

You could also watch for Input.txt file change and then call sed.

  • 2
    In-place edit won't work more than once because the 010707 then has vanished. – PerlDuck Jun 7 '18 at 17:51
  • How about using date for the output filename, e.g. >$(date +%F_%R)_Playlist.txt for 2018-06-07_12:00_Playlist.txt, 2018-06-07_16:00_Playlist.txtand so on – this way you don’t overwrite anything. – dessert Jun 7 '18 at 17:57
  • @Perl , it depends what's going to happen to the file in between. Orlando said he's going to copy it. I assumed that "Playlist.txt" may be overwritten after that. I'll update the answer considering the Orlandos' last comment. Thank You. – kcpr Jun 7 '18 at 19:04
  • @dessert , sure can do. Feel free to update the answer adding Your version of the script if You want. But please - remember about Windows and don't use : in the name of the file! ;) – kcpr Jun 7 '18 at 19:20

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.