Generating consecutive numbered urls

37 views Asked by At

I want to generate a text file containing the folowing lines:

http://example.com/file1.pdf
http://example.com/file2.pdf
http://example.com/file3.pdf
.
.
http://example.com/file1000.pdf

Can any one advise how to do it using unix command line, please?

Thank you

2

There are 2 answers

2
Raman Sailopal On BEST ANSWER

With an interating for loop

for (( i=1;i<=1000;i++ ));
do 
    echo "http://example.com/file$i.pdf";
done > newfile

With seq:

while read i;
do 
   echo "http://example.com/file$i.pdf";
done <<< $(seq 1000) > newfile
0
Alvan Caleb Arulandu On

It is possible to create/run a python script file ato generate this. Using vim, nano, or any other terminal editor, create a python file as follows:

def genFile(fn, start, end):
  with open(fn, "w+") as f:
    f.writelines([f"http://example.com/file{str(i)}.pdf\n" for i in range(start, end+1)])

try:
  fn = input("File Path: ") # can be relative
  start = int(input("Start: ")) # inclusive
  end = int(input("End: ")) # inclusive
  genFile(fn, start, end)
except:
  print("Invalid Input")

Once this is written to a file, let's call it script.py. We can run the following command to execute the script:

python script.py

Then, fill out the prompts for the file path, start, and end. This should result in all those lines printed in the file specified delimited by '\n'.