3

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?

8

Have you tried this

# cat a.txt 
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

# sed -e 's/\\/,/g' a.txt 

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
5

Here's a better way of writing the same script you tried:

#!/bin/bash

while read -r line
do
  echo "$line" | sed 's/\\/,/g'
done < "$1"

echo Done!

Of course, the whole thing could just be simplified to

#!/bin/bash

tr '\\' , < "$1"    
  • tr all the way - thats the quickest way to do it for sure. – mikeserv Apr 22 '14 at 15:38
3

Just make sure your shell doesn't eat those backslashes when pushing them to sed:

sed 's/\\/,/g' <<\BS
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
BS

#OUTPUT#

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
3

You could do it natively in bash - using either ${line/\\/,} to replace the first occurrence or ${line//\\/,} to replace all occurrences of \ in ${line} (equivalent of sed with the -g modifier):

while read -r line; do 
   echo "${line//\\/,}"
done < file.csv
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
2

Here comes an awk solution,

$ awk -F'\' '{print $1","$2}' file.txt
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.