Search code examples
bashawkterminalgawkstty

awk/gawk seems to overwrite columns on result


[TRIVIAL] [SOLVED]

tl:dr - DOW CRLF made to feel as though awk misbehaved; trivial mistake!

Am not sure why awk/gawk is behaving this way. Any hints? Ever seen awk results overwriting the columns on terminal? I am seeing this on just one machine; everywhere else, the stuff works just fine! Not sure if it's the terminal misbehaving or any other thing. An stty sane doesn't seem to help either. Anyone seen a similar thing?

 $ cat in.txt 
2132088 AAA_WEB_50181_552222195-1
2110665 AAA_WEB_50177_547796499-2
2157591 AAA_WEB_50181_551310844-1
2117370 AAA_WEB_50178_549250104-1
2109777 AAA_WEB_50180_551512637-1
2112704 AAA_WEB_50179_410838823-13
2116984 AAA_WEB_50178_549463801-2
2119511 AAA_WEB_50179_550076677-2
2122008 AAA_WEB_50180_551679428-1
2124984 AAA_WEB_50177_548026293-1
 $ awk '{print $2 " " $1}' in.txt 
 213208850181_552222195-1
 211066550177_547796499-2
 215759150181_551310844-1
 211737050178_549250104-1
 210977750180_551512637-1
 211270450179_410838823-13
 211698450178_549463801-2
 211951150179_550076677-2
 212200850180_551679428-1
 212498450177_548026293-1
 $ cat /etc/issue.net 
Ubuntu 14.04.4 LTS
 $ gawk '{print $2 " " $1}' in.txt 
 213208850181_552222195-1
 211066550177_547796499-2
 215759150181_551310844-1
 211737050178_549250104-1
 210977750180_551512637-1
 211270450179_410838823-13
 211698450178_549463801-2
 211951150179_550076677-2
 212200850180_551679428-1
 212498450177_548026293-1
 $ gawk -W version
GNU Awk 4.0.1
Copyright (C) 1989, 1991-2012 Free Software Foundation.

Screenshot: awk or terminal - misbehaviour


Solution

  • As pointed out by tripleee, the issue is likely due to DOS line terminators, a simple fix could be to strip the special characters using tr and feed it to awk for processing.

    < in.txt tr -dc '[:print:]\n' |  gawk '{print $2 " " $1}'
    

    In the above example, tr -dc '[:print:]\n' allows only the printable characters from the input file before feeding it to awk.