0
Role_Collections,Role_Name,Role_Template,App_Id
CLD:TEST1,Subaccount Admin,Subaccount_Admin,cis-local!b4

csvfile.csv I am reading above file using code below:

     {
read header
while IFS="," read -r Role_Collections Role_Name Role_Template App_Id
do
   #trim space before/after
    Role_Collections=("${Role_Collections%$'\r'}") | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'
    Role_Name=("${Role_Name%$'\r'}") | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'
    Role_Template=("${Role_Template%$'\r'}") | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'
    App_Id=("${App_Id%$'\r'}") | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'
    echo "Role coll=${Role_Collections}|${Role_Name}|${Role_Template}|${App_Id}"
    echo "${Role_Name} -to-role-collection  ${Role_Collections} -of-app   ${App_Id} -of-role-template ${Role_Template}" 
    
done
} < "csvfile.csv"

Output is below which is incorrect, why ?

Role coll=CLD:TEST1|Subaccount Admin|Subaccount_Admin|cis-local!b4
-of-role-template Subaccount_Adminn  CLD:TEST1 -of-app   cis-local!b4
Lord OfTheRing
  • 1,097
  • 3
  • 12
  • 20
  • Your code isn't a [mre] -- its behavior depends on the values to variables which aren't given any specific values, so how are we to say what behavior is right, wrong, or indifferent? – Charles Duffy Oct 15 '21 at 22:12
  • (in particular, whether your code or data contains DOS-formatted newlines and thus stray carriage returns is a critical question; a carriage return sends the cursor to the left of the screen when printed, such that subsequent characters overwrite whatever was in that position previously) – Charles Duffy Oct 15 '21 at 22:14
  • 1
    ...adding `set -x` to your script to enable trace logging is a good place to start. (Running it with `bash -x yourscript` also achieves that end). – Charles Duffy Oct 15 '21 at 22:14
  • This data coming from .csv file with ending end of the line character(CRLF). Is that what causing issue ? I am new to sh and Unix special characters and how it is treated. How do I treat my .csv file (Windows) to same as Unix ? – Lord OfTheRing Oct 17 '21 at 14:48
  • Answers on the linked duplicate describe that. They range from running the file through `dos2unix` or `tr -d '\r'`, to using bash parameter expansion syntax (`"${varname%$'\r'}"`) to strip a trailing carriage return after the data is already in a shell variable. – Charles Duffy Oct 17 '21 at 19:03
  • I have updated my question with full code and csv file format. Still I am not able to print my second line correctly. – Lord OfTheRing Oct 18 '21 at 17:30
  • I was able to fix it by removing "sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'" from code. Some reason it was causing problem. – Lord OfTheRing Oct 18 '21 at 18:40

0 Answers0