0

I have a requirement where I need to append values to a global string and add a new line character after each line.

iput data looks like below

a,b
1,2
3,4
5,6
str_val=""


for file in $input_dir*;do
 echo $file
  while IFS=, read -r col1 col2
  do
    str_val+="${col1}_${col2}\n"
  done < $file
done

echo $str_val

above is what I came up with so far and it is printing only the last value \n3_4

can anyone tell me where am I doing wrong?

  • when I run your code I get: `echo $str_val` => `a_b\n1_2\n3_4\n5_6\n` ... as expected; if the file contains windows/dos line endings (`\r\n`) then I get: `echo $str_val` => `\n5_6` ... also as expected; at this point I'm unable to generate the same `\n3_4` so I'm guessing there could be some odd/non-printing characters in your input file; please update the question with the output from `od -c file` so we can see exactly what's in your input file – markp-fuso Aug 18 '22 at 15:41
  • Remember that when the `\r` character is printed, it sends the cursor back to the beginning of the line; so printing `123\r456\r789` shows only `789`, even if the whole string is stored. – Charles Duffy Aug 18 '22 at 18:17

0 Answers0