I'm not sure how to phrase the question as most of the answers are about removing \r\n
from a file.
I have a unique problem where compressed files are numbered randomly and in order to associate them properly with a database record, I need to list the file contents and check them.
I'm using this solutions "In bash script, how to capture stdout line by line"
Which was a great start.
Some of the contents have a name with whitespace and I found this solution: How to print third column to last column?
I discovered when attempting to update the database record, that ^M
is being inserted in the results from of the awk
pipe, but only for the NF
column.
Not sure how to resolve this particular glitch. I don't see where ^M
is being
inserted, or how to remove it from the last column.
My code
This line works fine if I strip ^M
filename="$(echo "$line" | awk '{if ($3 ~ /^M$/) {sub(/^M$/,"", $3)} printf $3; printf ""}')"
This line fails:
text="$(echo "$line" | awk '{for(i=6;i<NF+1;i++) {if ($i ~ /^M$/) {sub(/^M$/,"", $i)} } printf "%s ", $i; printf ""}')"
And the simplified version fails:
text="$(echo "$line" | awk '{for(i=6;i<NF+1;i++) sub(/^M$/,"", $i) printf "%s ", $i; printf ""}')"
In vim
/vi
^M
is created with ctrl-V + <return key>
Using \r\n
has no effect.
I'm using cygwin
, and have been for a long time, and I have other *nix
scripts that I have written which run fine. I discovered that for some reason, this particular run of awk
is adding ^M
to the output.
I found this question with a similar problem, but I created my script with vim
from the start so there was no windows based editor involved.
If I mount that windows folder as a samba share and run the script from linux
it produces the output without a ^M
, so at this point I'm wondering if this is a bug or something else. It's really strange.
UPDATE My use of the REGEX in sub() was causing the string to return empty, so I did not properly understand how to clear out the CRLF.
NF+1 was a leftover from attempting to find out what was introducing the CRLF I was using i<=NF before that.
dos2unix
does. – Kusalananda Jan 24 '20 at 07:22