I'm having some difficulty determining where my syntax is wrong with an awk statement. My data looks like this:
AAAA777
AAADMD
AAAEEE
AAALAWN
AAAMAN
AAA7777
I want the output to be left justified, padded to 8 total characters, plus a couple of add-ons at the end. The desired output would look like:
AAAA777 ZC#8
AAADMD ZC#8
Instead what I'm getting looks like:
ZC#8777
ZC#8D
ZC#8E
ZC#8AWN
ZC#8N
ZC#8777
My awk statement looks like this:
awk '{ printf "%-8.8s%2.2s%2s\n",$0,"ZC","#8" }' file.txt
It seems backward and it's not the desired character count. What am I doing wrong?
The problem appears to be that your file has DOS/Windows line endings. I created file.txt without them and it worked fine:
However, if I convert the line endings to DOS, then I get the output that you see:
Unless you want to keep the DOS line endings, the straightforward solution is to remove them with any of the usual utilities, such as
dos2unix
ortr
.Alternatively, use awk's record separator,
RS
, to remove the unneeded carriage return: