1

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
l0ckm4
  • 757
  • 5
  • 17
  • So what's preventing you from adding `"_NA_NA_$(date +%s).TIFF"` to the string you have? What's your actual question? – tripleee Feb 07 '18 at 12:16
  • It doesn't work for some reason - please see update above – l0ckm4 Feb 07 '18 at 13:27
  • Do you have DOS carriage returns in the input file, or the script, by any chance? – tripleee Feb 07 '18 at 13:49
  • Quite possibly - it is a file created in DOS believe it or not :-) – l0ckm4 Feb 07 '18 at 14:09
  • Happy to accept as a duplicate if I knew how to - it was def the line endings. Thank you for your help – l0ckm4 Feb 07 '18 at 14:22
  • Normally when somebody with 2k+ rep flags as duplicate it asks you if that's correct and then that's it. I can't do that because I have enough [tag:bash] rep that it will unilaterally close the ticket when I nominate it as duplicate, and I didn't want to do that until we had confirmed that this was indeed the problem. All good now, thanks for your quick response. – tripleee Feb 07 '18 at 14:33

1 Answers1

0

What about:

name="$(head -n1 inputFile)_NA_NA_$(date +%s).TIF"
M. Becerra
  • 639
  • 6
  • 21