I am trying to read the first line from a CSV file and then from the two entries build up a string to be used as a filename later on. The code I have is as follows:-
#Read TXT File to get Plan Number and Letter Type
while IFS=, read -r plan lettype
do
p="${plan/\//~}"
q="$lettype"
r="${p}_$q"
echo $r
done < "$j"
$r
is echoing out 320529~14_NEA
at the moment.
the first line of the csv file is normally something like 320529/14,NEA
and I need to get to something like this into a variable
320529~14_NEA_NA_NA_131624733749396662.TIF
The _NA_NA_
and .TIFF
are static and the long number is some kind of timestamp but for the moment I am happy to work with the output from $(date +%s).
--Update-- Thanks for the two suggestions so far - neither work though. This is what I have tried.
if [ -f $j ]; then
echo "File $j exists."
name="$(head -n1 ${j})_NA_NA_$(date +%s).TIFF"
echo $name
#Read TXT File to get Plan Number and Letter Type
while IFS=, read -r plan lettype
do
p="${plan/\//~}"
q="$lettype"
r="${p}_$q_NA_NA_$(date +%s).TIFF"
s="${p}_${q}_NA_NA_$(date +%s).TIFF"
echo $r
echo $s
done < "$j"
else
echo "File $j does not exist yet."
fi
outputs
File ./GG11HH1J.TXT exists.
_NA_NA_1518009847.TIFF
320529~14_1518009847.TIFF
_NA_NA_1518009847.TIFF
--Update2-- Thanks to @triplee I now have the answer. it was a DOS line ending. Stripping it out and it all worked as expected. Code used was:-
fname=$(cat $j | sed 's/\r$//')
fname="${fname/\//~}_NA_NA_$(date +%s).TIFF"
fname=${fname/,/_}
echo $fname