Issue:Cut command giving wrong results on cutting last field
While processing data from a delimited file in bash/shell script using cut command, you might run into peculiar issue. The last field does not cut properly.
The output of cutting the last field will give you some junk character and it might break your code to something quite unexpected.
If you don't know whats causing this, the situation can be very tricky and frustrating.
So, the secret is the file was edited in window mode and it adds carriage return character. So, now you the root cause, the solution is not very difficult either.
#1. You can use unix to edit the command. This may not help when you are using CSV files with MS excel.
#2. Alternatively, Copy-paste or edit the file in EditPlus or similar text editor, and edit and save in unix mode.
#3. Use tr command to trim the '\r' character. (tr -d '\r'). You should anyway do it to be on the safer side even if you are doing one of the above step. Just in case, some of your friend wanna use your script.
Here is a demo script, showing the example how to use it in a bash script:
#! /bin/bash #Purpose: Demo Script echo "Started processing the input file: $1" INPUT_FILE_NAME=$1 cat /dev/null > $2 count=0 while read LINE do let count++ cust_id=`echo $LINE | cut -d"," -f1` itemName =`echo $LINE | cut -d"," -f2` paymentStatus =`echo $LINE | cut -d"," -f3` invoice=`echo $LINE |tr -d '\r'| cut -d"," -f4` echo "Record:'$count': cust_id='$cust_id':itemName='$itemName':paymentStatus='$paymentStatus':invoice='$invoice'" done < $INPUT_FILE_NAME echo "Finished processing the input file: $1" fi
No comments:
Post a Comment