While I posted my answer as a comment and comments were removed, let's give it another shot:
The way you are approaching it is OK but can be improved in such a way that you won't need the dependency on too many external binaries. For example, to solve your issue you can use awk
as follows:
awk -F, -vmyvar="ValNeeded" 'NR==2 { if ($4 == myvar) print "match"; else print "No match"}' file.csv
If you want to keep your code but rewrite it slightly it'd look like this:
#!/bin/bash
varcsv=$(awk -F, 'NR==2 { print $NF }' file.csv)
myvar=ValNeeded
if [[ $varcsv == "$myvar" ]]; then
echo "true"
else
echo "false"
fi
The $NF
there gets the last field in the record.
As it was already mentioned by Philippos, your file contains MSDOS line endings, a.k.a CR/LF.
You can check those in different ways using, cat
, sed
, od
and many other tools but let's keep it simple and use cat
and sed
in this case:
cat -vEt file.csv
or sed -n l file.csv
This will return something like:
Val1,Val2,Val3,SomeVar^M$
Val1,Val2,Val3,ValNeeded^M$
Val1,Val2,Val3,Ignorevar^M$
$
To remove these ^M$
charachters all together, you can use some utils. For example:
dos2unix file.csv
. There are other ways to do it from within the editor but that requires more effort.
Once you've converted the file you should be good.
cat´ version supports the
-v` option, please share the output ofcat -v file.csv
fromdos
. or if you don't have that installed, you can useperl -i -p -e 's/\r\n/\n/' filename.csv