Somewhere before the line count=~echo "$conflict" | wc -l~
(~ are used in place of back quote), the hex character 0x0A
is being added. So, when the files $DA
and $DB
are the same (meaning the script should return a null set), 0x0A
is being added. So, instead of the line count being zero in the next command, the count is 1. It is being added to the end the output for each iteration of the loop. It isn't a big deal for loops returning other content as well but is an annoyance for loops in which a null set should be returned (and accordingly, a new file should not be created). I have experimented with inserting tr -d '\r'
and tr -d '\n'
at the start and end of the conflict=~cat "$DA" "$DB"
line but it hasn't helped.
How can I simply delete any line that contains only this character (or really any non-printable character except for \n
)?
Your help is appreciated.
# Files "DA" and "DB" are titled as such:
# 10M_$i_$j_$m_OtherNonRelevantChars or 10M_$i_$k_$n_OtherNonRelevantChars
# Field one is an integer; fields 2 and 3 are alphanumeric (checksum hashes)
IFS=$'\n'
for i in {1..7}
do for j in {P,B,R}
do for k in {P,B,R}
do for m in {3,9}
do for n in {3,9}
do
DA=`ls "10M_${i}_${j}_${m}"*`
DB=`ls "10M_${i}_${k}_${n}"*`
dos2unix "$DA" "$DB"
conflict=`cat "$DA" "$DB" | \
awk -v OFS=',' -F',' '{print $1,substr($2,0,8),substr($3,0,8)}' | \
sort -t',' -k1,1n | uniq -u | \
awk -v i=$i -v j=$j -v k=$k -v m=$m -v n=$n -v OFS=',' -F',' '{print i,j,m,k,n,$1}' | \
uniq -d`
# The unwanted character appears somewhere before here
count=`echo "$conflict" | wc -l`
if [[ "$count" -gt 0 ]]
then
echo $conflict >> "C_${i}_${j}_${m}_${k}_${n}.txt" # output of conflicting hashes for given matching unique first field ID
fi
echo "$i $j $m $k $n"
done
done
done
done
done
\n
intr
, sotr -d '\n'
should work. There is so much other stuff going on though that it's hard to tell which part is failing. If you can write a minimal, reproducible example you'd have a much better chance of getting an answer.tr -d '\n'
should work, something about my setup (or my system) has resulted in continued failure. I am dumbfounded. In this case, I decided to implement a workaround and give-up on solving the root problem. In this case, I changed... | wc -l
to... | wc -c
and changed the if-then condition to... -gt 1
. This solves my specific problem without have to solve whytr -d '\n'
isn't working as expected.