0

lets say I have a file called somedata.txt, I am trying to replace:

\"

with

"

i.e. removing the \ I am trying to do this using AWK. Can you please help?

Charlie C
  • 3
  • 1

2 Answers2

2

You will need to escape both characters \ and " like shown below

awk '{gsub("\\\\\"", "\"")}1' somedata.txt

Or if you can use sed it becomes simpler

sed 's/\\"/"/g' somedata.txt

sed also allows inplace editing (flag -i) to update original files

sed -i 's/\\"/"/g' somedata.txt
ppp
  • 975
  • 7
  • 15
  • In awk slashed regexp not quoted string containing regexp gets closer: `awk '{gsub(/\\"/,"\"")}1'` or `awk -vq=\" '{gsub(/\\"/,q)}1'` and [_recent gawk_ has inplace](https://stackoverflow.com/questions/16529716/awk-save-modifications-in-place/) – dave_thompson_085 Jun 04 '17 at 01:59
  • Thank you both. @Pradip I've never used sed, is there a way to combine two find/replace and update the original file? I also need to replace \n with a new line. I was achieving that with awk using: awk '{gsub(/\\n/,"\n")}1' somedata.txt – Charlie C Jun 04 '17 at 02:21
  • @dave_thompson_085: Thanks for pointers @CharlieC: Multiple find/replace expressions can be combined using semicolon `;` (in both `sed` and `awk`) In this case, `awk -i inplace '{gsub(/\\"/,"\""); gsub(/\\n/,"\n")}1' somedata.txt` – ppp Jun 04 '17 at 03:23
1

You can also open file in vim editor and try this: Press esc and then

:%s_oldString_newString_g

For your case, it's

:%s_\\\"_\"_g

Since " and \ are special characters, you need to add a \ before the chars to make the command execute.

Julina
  • 709
  • 5
  • 13