Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
6  
That's what tr's for: tr '\\' , –  Stéphane Chazelas Apr 22 at 6:55

5 Answers 5

up vote 7 down vote accepted

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
share|improve this answer
    
@Abhishek - Dhanyawaad! –  Mandar Shinde Apr 22 at 4:05
    
@MandarShinde Pleasure is all mine. –  Abhijeet Kasurde Apr 22 at 4:32

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"    
share|improve this answer
    
tr all the way - thats the quickest way to do it for sure. –  mikeserv Apr 22 at 15:38

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
share|improve this answer

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
share|improve this answer

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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