Unix sed can't read. peculiar issue when using a variable

0

One line in a control file looks like this: 1039 ZA_BKPF_20170725103534.txt So using space as a delimiter, I am cutting and assigning the filename alone to a variable like below: file=echo "$line" | cut -d " " -f2

and then I use sed: sed -i 1d "$targetDir"/"$file"

but in the log I am getting the below error: I did an echo as well to confirm if the file exists. As you can see, the file exists. but sed says no such file or directory.

  • file=$'ZA_BKPF_20170725103534.txt\r'
  • echo '/u/applic/data/hdfs1/etl_grmacw/ACDW/source/ZA/ZA_BKPF_20170725103534.txt exists' /u/applic/data/hdfs1/etl_grmacw/ACDW/source/ZA/ZA_BKPF_20170725103534.txt exists
  • sed -i 1d $'/u/applic/data/hdfs1/etl_grmacw/ACDW/source/ZA/ZA_BKPF_20170725103534.txt\r' sed: can't read /u/applic/data/hdfs1/etl_grmacw/ACDW/source/ZA/ZA_BKPF_20170725103534.txt: No such file or directory

Is someone familiar with the $'string\r'. why is this happening to my variables. Is that the problem or something else? Can someone help me pls ! Thanks, Hari G

Hari Gopinath

Posted 2017-07-26T15:12:02.793

Reputation: 1

2It looks as though your control file is in Windows format. The $'...' format is just being used for reporting purposes, to show that there is a return (0x0D) character in the variable. You can use tr to filter it out, – AFH – 2017-07-26T15:34:46.317

1... or use dos2unix on the input file to clean it up. – xenoid – 2017-07-26T20:03:13.930

No answers