Home > OS >  awk includes newline characters
awk includes newline characters

Time:06-28

I have a textfile with a bunch of data and lines like SID: 1 - SN: 0123456789 scattered all over the file. All lines are delimited with CR/LF (Windows)
In bash I create an array with unique Serial Numbers:

sn=($(cat ./serials |awk '/SN: / { print $3 }' FS=': '|sort -u;))

So far so good, but each array member contains a CR at the end:

echo "${sn[0]}:test"

prints :test56789 instead of 0123456789:test

I can fix it with tr -d '\r' like this:

sn=($(cat ./serials |tr -d '\r'|awk '/SN: / { print $3 }' FS=': '|sort -u;))

but I doubt if this is the best approach. Is there a way to remove the CR in the awk command?

CodePudding user response:

Is there a way to remove the LF in the awk command?

Sure you can have awk like this:

awk -F ': *' '{sub(/\r$/, "")} /SN: / {print $3}' serials

Your complete solution to read awk output into a bash array:

readarray -t sn < <(
awk -F ': ' '{sub(/\r$/, "")} /SN: / {print $3}' serials | sort -u)

# check bash array
declare -p sn

CodePudding user response:

{m,g}awk '$!NF = $3' FS=':[ ]*' RS='\r?\n'
  • Related