tester tester - 24 days ago 6
Bash Question

Replace by removing new line character using AWK in shell?

I have a file:

a.txt

f1,f2,f3
a,b,c
d,e,f
g,h,i


I want to concatenate f2 and f3 by removing the newline character in f3 [ie. f2=f2.f3]and update the file. I tried the command below, but the new line character is getting inserted as well. How do I remove that part?

awk -F "," -v OFS=, '{$2 = $2$3;print}' abc.txt > abc.tmp && mv abc.tmp abc.txt

Answer

This is due to presence of DOS line feed \r at the end of line.

You can use this gnu awk command with RS as \r\n to take care of that:

awk -v RS='\r\n' 'BEGIN{FS=OFS=","} {$2 = $2$3} 1' a.txt

f1,f2f3,f3
a,bc,c
d,ef,f
g,hi,i

Alternatively, you can run dos2unix command before using your regular awk command:

dos2unix a.txt
awk 'BEGIN{FS=OFS=","} {$2 = $2$3} 1' a.txt