Append text to end of every line

I've scoured the internet with mixed results. As an amateur I turn to the great minds here.

I have a text file of 80 or so lines. I want to add ".pdf" to the end of each line. (For now that's it)

Most of the internet points toward using "sed". I don't know coding but can figure things out (enough to paint myself into a corner). The closest I've gotten is:

sed 's/$/.pdf/' input

which will print within the shell:

lastlineofinput.pdf

instead of appending ".pdf" to each line within the file.

awk '{NR==1?s=$0 : s=s".pdf"$0}END{print s}' input.txt
did the same thing.

Operators are largely beyond me. I see different solutions involving different amounts of single and double quotations and I'm not sure which ones I should use.

I have had success with finding and replacing text using sed.

sed s/'text1'/'text2'/g inputfile.txt > outputfile.txt

that's a lot more than I knew before today, though admittedly more straightforward (it appears).

The closest I came was one bit of code (I forgot which now) that inserted a period to the end of the very last line. Nowhere else.

My goal is to go from this:

/02/pdf/01
/02/pdf/02
/02/pdf/03
/02/pdf/04
/02/pdf/05
/02/pdf/06

to this:
/02/pdf/01.pdf
/02/pdf/02.pdf
/02/pdf/03.pdf
/02/pdf/04.pdf
/02/pdf/05.pdf
/02/pdf/06.pdf

I'm on a Mac using Terminal so I don't know what I'm doing and appreciate any help. Thank you!

awk '{ print $0 ".pdf" }' < input > output

Hi,

Your 'sed' command works in my Linux box. It's an easy work for Perl too.

$ cat infile
/02/pdf/01
/02/pdf/02
/02/pdf/03
/02/pdf/04
/02/pdf/05
/02/pdf/06
$ sed 's/$/.pdf/' infile
/02/pdf/01.pdf
/02/pdf/02.pdf
/02/pdf/03.pdf
/02/pdf/04.pdf
/02/pdf/05.pdf
/02/pdf/06.pdf
$ perl -ne 'chomp; printf "%s.pdf\n", $_' infile 
/02/pdf/01.pdf
/02/pdf/02.pdf
/02/pdf/03.pdf
/02/pdf/04.pdf
/02/pdf/05.pdf
/02/pdf/06.pdf

Regards,
Birei

Thank you both for the quick replies. Using:

awk '{ print $0 ".pdf" }' < input > output

and

$ perl -ne 'chomp; printf "%s.pdf\n", $_' infile

both output:

/02/pdf/06.pdf

within Terminal on my first machine.

I switched to a portable (Mac) and it output the appended .pdf after each line (an improvement) within Terminal but did not modify the original input or the new output files.

Am I missing something?

Is that not what you asked for? :confused:

In any case you can't have typed this literally:

awk '{ print $0 ".pdf" }' < input > output

...because the highlighted part redirects its output into the 'output' file, not the terminal.

awk, and most other utilities, can't read from and write to the same file at the same time. If you want to do that, overwrite the original file with the new one, once you've checked the data is still okay.

'sed -i' can edit files "in place", but is only available in Linux, can have the unfortunate side-effect of changing the ownership of the file, and -- as noted above -- if you make a mistake with it, you've wrecked your original data.