data_addict
data_addict

Reputation: 894

Script to remove slash

I need to search for the combination of back slash without code character and if found, I need to remove the back slash. I know replace functions like below, however I don't know how to remove characters.

awk -v RS='"[^"]*"' -v ORS= '{gsub(/\n/, " ", RT); print $0  RT}' file.csv

This I need to do on a csv file.

Input:

id,name,address
1,A,First Address
2,B, Second \,Address
3,c, ThiRd \" Address 

Output:

id,name,address
1,A,First Address
2,B, Second ,Address
3,c, ThiRd \" Address 

Sample Input

id,name,address
1,A,F/,irst /Address
2,B, /Second /,Address
3,c, //ThiRd /" Address

As per Script got below output

id,name,address
1,A,First ddress
2,B, econd Address
3,c, ThiR/d \" Address

Output

id,name,address
1,A,F,irst Address
2,B, Second ,Address
3,c, ThiRd /" Address

Kind of this

\(?!") -> remove back slash

Upvotes: 0

Views: 97

Answers (2)

stack0114106
stack0114106

Reputation: 8761

Using perl, this can be solved easily by Negative lookahead. I used your "Sample Input" as my input and got the required output as in your "Output"

> cat input_file.dat
id,name,address
1,A,F/,irst /Address
2,B, /Second /,Address
3,c, //ThiRd /" Address
> perl -pe ' $_=~s#/(?!")##g ' input_file.dat
id,name,address
1,A,F,irst Address
2,B, Second ,Address
3,c, ThiRd /" Address
>

Upvotes: 0

RavinderSingh13
RavinderSingh13

Reputation: 133710

EDIT: Seems you have changed slash to \ so adding this now.

awk '{for(i=1;i<=NF;i++){if($i ~ /\\[^"]/){sub(/\\/,"",$i)}}} 1' Input_file

Following will be the code's output when I run it.

awk '{for(i=1;i<=NF;i++){if($i ~ /\\[^"]/){sub(/\\/,"",$i)}}} 1'  Input_file
id,name,address
1,A,First Address
2,B, Second ,Address
3,c, ThiRd \" Address

In case you want to remove all occurrences of / then use following.(seems your output still shows 1 slash so only mentioning this)

sed 's#/##g' Input_file

Upvotes: 2

Related Questions