0

Tried with the command but first line is replaced with comma

awk '{if(NR==1){print $0","}else{print }}' /home/a/b/file1.txt
a 
b
c

Expected result

a,
b
c
Dominique
  • 16,450
  • 15
  • 56
  • 112
Jerin J
  • 75
  • 1
  • 5

1 Answers1

1

If you have CRLF endings you need to convert them to LF ones, see How to convert Windows end of line in Unix end of line (CR/LF to LF).

Then, your solution may work as is. Alternatively, you may use

awk '{ print NR==1 ? $0"," : $0 }' /home/a/b/file1.txt > newfile

Here, you only print the whole line with a , at the end if it is the first line, else, you only print the line itself.

If you want, you may also do it with sed:

sed '1s/$/,/' /home/a/b/file1.txt > newfile

See the online demo. Here, sed replaces the end of line position with , on the first line (so, basically adds the comma). If you use sed '1s/,*$/,/' you may make sure there is only 1 comma at the end of the first line in the result.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563