Array grabbing return character in element variable


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.


## 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/

## 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

## Echo back the selection result
echo $myFile


## Script creation done

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

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

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


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

while read -a arry

  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


Fixed it with this:

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

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.