Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

awk includes newline characters

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading