Thursday, May 5, 2022

[SOLVED] Add field to text file in Linux using awk

Issue

I need to add |1 to all lines in a text file.

I do not have write access to the file, so I need to create a new file. I am using the command;

awk '{print $0 "|1"}' /sas/old_file.TXT >> /sas/new_file.TXT

which works perfectly except that it inserts the |1 in a new line. I tried the exact same command with a text file I created, and it worked fine. So my suspicion is that the file contains a line feed character at the end of each line, and when a new field is inserted, it does so after the line feed character. Any ideas how to circumvent this please?


Solution

Lines in a Windows text file end in \r\n. Since awk only processes text, it is surprising that it doesn't open files in text mode by default. To change the record separator to Window newlines, you can do:

awk 'BEGIN {RS="\r\n"} {print $0 "|1"}' /sas/old_file.TXT >> /sas/new_file.TXT


Answered By - stark
Answer Checked By - Dawn Plyler (WPSolving Volunteer)