1

I have a requirement of appending the string " 1" at the end of every line of the file. After the execution of below two commands, an empty file is created.

awk '{ print $0 " 1" }' < file.txt > file.txt 

using sed

sed 's/$/ 1/' file.txt > file.txt

What is the mistake attempted here, can you please advise ? I am executing these commands in mac terminal.

Prradep
  • 203
  • Thanks for that, I learnt that as well. Could you please help me in my query which is different 'not able to append the string at the end of each line' ? Thanks – Prradep Jul 31 '15 at 12:40

1 Answers1

3

The GNU sed solution needs -i for inline editing:

sed -i 's/$/ 1/' file.txt

(With BSD sed, you need sed -i '')

In awk, you have to work around that with a temporary file:

awk '{ print $0 " 1" }' file.txt > tmp && mv tmp file.txt

In fact, sed also creates a temporary file, but -i handles that

The Problem with awk '{ print $0 " 1" }' < file.txt > file.txt:

The redirections were performed before awk get the control. > file.txt will truncate the file. Try in a terminal:

>file

The file will be zero sized, even if before the file had contents. So awk has no data to read from that file. That's the same problem as with sed.

Final solution with \r line breaks:

perl -pi -e 's/\r\n|\n|\r/ 1\n/g' file.txt
chaos
  • 48,171
  • I assume that I can't use the same filename for both input and output. So i tried the following two commands using awk awk '{ print $0 " 1" }' < file.txt > file_new.txt and awk '{ print $0 " 1" }' < file.txt > tmp && mv tmp file.txt. The string has only been appended to the last line of the files not to every line. Meanwhile sed command sed -i'' 's/$/ 1/' file_IDs.txt gave an error sed: 1: "file_ ...": extra characters at the end of f command. Do the file shouldn't contain _ in it's name? – Prradep Jul 31 '15 at 12:33
  • @Prradep sry, the sed command in osx needs a space between -i and '', I updated the answer – chaos Jul 31 '15 at 12:41
  • Thanks @chaos. After changing the sed command, the output is same like the awk command. Only the last line of the file is updated with the string pattern not every line . – Prradep Jul 31 '15 at 12:45
  • @Prradep I tested it, for me it adds the string to every line. Can you give a sample of your file to test? – chaos Jul 31 '15 at 12:46
  • Yeah, it worked fine for test file which i created myself but not for the real file. $ head file.txt 123 123 2334 345 345 23456 123 123 2334 345 this is the test file. When use head real_file.txt it is printing only the last line as output. As a beginner, I have no idea on the error. – Prradep Jul 31 '15 at 12:56
  • @Prradep Your real_file.txt has only one line. Therefore head prints only that one line and the awk/sed command add only one string. – chaos Jul 31 '15 at 12:58