A.DEV
A.DEV

Reputation: 11

AWK field variable (e.g. $1) creates new line? Concatenation issue

awk '
 BEGIN {
 FS = ">";
 insertStatement = "Rating:";
}

/<Overall Rating>/{

 insertStatement = insertStatement $2 "percent";
 printf insertStatement;
}
' $file

This code produced the following output:

percent4

Expected:

Rating:4percent

I am presuming $2 produces a new line? Any idea how to fix this?

Upvotes: 1

Views: 150

Answers (1)

dave_thompson_085
dave_thompson_085

Reputation: 38990

$<n> (such as $2) does not 'create' a new line, and you don't have a new line. You apparently have overstriking on the existing line, usually caused by a CR character. You can see this by piping the output through cat -v or sed -n l or similar.

If your data file contains CRLF line endings instead of Unix-standard LF, as is frequently the case for files processed on or even transferred through Windows systems or applications and often the case for files or data downloaded from websites or FTP*/SFTP sites, awk does treat the CR characters as the last character of the line and the last character of the last field on the line. If $2 is the last field on that line, then yes $2 will include the spurious CR character and cause this problem.

Crossdupe: https://unix.stackexchange.com/questions/312446/using-awk-to-make-new-file-results-in-issues-using-1-specific-column-cant-fig

To fix:

  • get an input file that already has LF not CRLF

  • fix the line endings in in the file with the specific dos2unix program or any number of equivalents using tools like tr sed etc

  • remove the CR in awk with sub(/\r$/,"") or equivalent

Upvotes: 2

Related Questions