I tried writing a script in which I am reading through a file line-by-line. I am supposed to replace a backslash (\) with a comma (,).
The input file is as follows:
1,2,3,WORKSTATION1\A,5 1,2,3,WORKSTATION2\B,5 1,2,3,WORKSTATION1\C,5 1,2,3,WORKSTATION1\D,5 1,2,3,WORKSTATION1\E,5 1,2,3,WORKSTATION1\F,5 1,2,3,WORKSTATION1\G,5
The piece of code I've written is as follows:
#!/bin/bash cat $1 | while read -r line do ln=$(echo $line | xargs | sed 's/\\/,/g' ) echo $ln done echo Done!
When I run
./script.sh file.csv, I am getting following output:
1,2,3,WORKSTATION1A,5 1,2,3,WORKSTATION2B,5 1,2,3,WORKSTATION1C,5 1,2,3,WORKSTATION1D,5 1,2,3,WORKSTATION1E,5 1,2,3,WORKSTATION1F,5 1,2,3,WORKSTATION1G,5 Done!
Whereas, I am expecting an output as follows:
1,2,3,WORKSTATION1,A,5 1,2,3,WORKSTATION2,B,5 1,2,3,WORKSTATION1,C,5 1,2,3,WORKSTATION1,D,5 1,2,3,WORKSTATION1,E,5 1,2,3,WORKSTATION1,F,5 1,2,3,WORKSTATION1,G,5 Done!
I also tried replacing
sed 's/\\/,/g' with
tr "\\" ",", but it is not helping. Any modifications needed?