Search code examples
perltextsed

Adding incremental number at end of every line in text file


I have a text file with URLs in separate lines

www.example1.com 
www example2.com 

I want to add some prefix and suffix to every URLs in text file.

gallery-dl -g www.example1.com > link1.txt
gallery-dl -g www.example2.com > link2.txt

How I can do this with Perl with command line. I tried with sed but failed. I am on Ubuntu.

I tried this

$perl -ne 'chomp;print "gallery-dl -g $_ > link$..txt\n"' urls.txt

but > link1.txt went into 2nd new line.


Solution

  • You are passing a Windows text file (CRLF-terminated lines) to a unix build of Perl (which expects LF-terminated lines).

    Convert your file to a unix file (e.g. using dos2unix) or handle CRLF line endings (e.g. by replacing chomp with s/\s+\z//).