Reputation: 11
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
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.
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