Array grabbing return character in element variable

agetz
Contributor

I have been working on this script for a while now and I am to the last issue and can't get past it. The CSV i'm using has returns (seems like) at the end of each line. When the element is echoed out to a new file, it is including that in the value, so it is skipping a line. Any help would be much appreciated.

#!/bin/bash

## We capture the logged in user and the UID to use later when running the local script as the user
loggedInUser=$(stat -f%Su /dev/console)
loggedInUID=$(id -u "$loggedInUser")

## Create a script in /tmp to run as the logged in user
cat << EOS > /private/tmp/file_chooser.sh
#!/bin/bash


## Run an Applescript to present file chooser to the user

osascript -e 'tell application (path to frontmost application as text)
set myFile to choose file
POSIX path of myFile
end'



## Echo back the selection result
echo $myFile

EOS

## Script creation done

## Now make the new script executable
chmod +x /private/tmp/file_chooser.sh

## Run the script as the user, capturing the output into a new variable
FileChooserResponse=$(/bin/launchctl asuser "$loggedInUID" sudo -iu "$loggedInUser" "/private/tmp/file_chooser.sh")

Date=`date "+%Y%m%d%H%M%S"`

file_in="$FileChooserResponse"
file_out="/Users/"$loggedInUser"/Downloads/Frontline_Time_$Date.xml"




echo '<?xml version="1.0"?>' > $file_out
echo '<TAARecords>' >> $file_out
#while IFS=$',' read -a arry
#do

IFS=","
while read -a arry
do

  echo '  <TAARecord>' >> $file_out
  echo '    <EmployeeID>'${arry[0]}'</EmployeeID>' >> $file_out
  echo '    <JobNumber>'${arry[1]}'</JobNumber>' >> $file_out
  echo '    <ItemNumber>'${arry[2]}'</ItemNumber>' >> $file_out
  echo '    <RegHours>'${arry[3]}'</RegHours>' >> $file_out
  echo '    <OTHours>'${arry[4]}'</OTHours>' >> $file_out
  echo '    <BatchID>'${arry[5]}'</BatchID>' >> $file_out
  echo '    <Date>'${arry[6]}'</Date>' >> $file_out
  echo '  </TAARecord>' >> $file_out
done < $file_in
echo '</TAARecords>' >> $file_out
2 REPLIES 2

agetz
Contributor

Fixed it with this:

  echo '    <Date>'${arry[6]//$'
'}'</Date>' >> $file_out

tlarkin
Honored Contributor

FYI awk and sed can strip out carriage returns as well.

echo $string | sed $'s/
//'

You can also strip them all out of the entire document using things like tr to begin with. You can also request that the person sending you the CSV uses UTF-8 text encoding, which I think should strip out any rich text stuff. however, if you are getting these from MS Office 365 they might just have those carriages in them no matter what.