0

I want to replace fw[" with fw..

I have tried couple of sed commands, but couldn't solve the issue.

I tried cut command, tr and sed commands. But since the characters I like to replace are special characters, I get lot of errors.

This is my input:

endpoint.os.version="Windows 10"
endpoint.fw["MSWindowsFW"].version="10.0"
endpoint.av["AlwilAV"].version="11.1.2253"
endpoint.os.hotfix["KB3116278"]="true"

This is the output I want:

endpoint.os.version="Windows 10"
endpoint.fw.MSWindowsFW.version="10.0"
endpoint.av["AlwilAV"].version="11.1.2253"
endpoint.os.hotfix["KB3116278"]="true"  

Can you help me to write such a transform?

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Bala
  • 11
  • 3
  • 1
    What kind of input do you work with? A stream? One or multiple files? A Variable? Short hint: escape (ie.: `echo 'fw["' | tr -d '["'`). – bufh Apr 11 '16 at 12:49
  • Hi! it is just one file has 4 lines (inputs) – Bala Apr 11 '16 at 12:55

1 Answers1

1

Assuming file.txt:

endpoint.os.version="Windows 10"
endpoint.fw["MSWindowsFW"].version="10.0"
endpoint.av["AlwilAV"].version="11.1.2253"
endpoint.os.hotfix["KB3116278"]="true"

As POSIX regular expressions are greedy, you could do it like this with Perl:

perl -pe 's/fw\["(.*?)"\]/fw.\1/g' file.txt

Or in pure sed:

sed 's/fw\["\([^"]*\)"\]/fw.\1/g' file.txt

Note: I recommend you to used https://regex101.com/ or https://www.debuggex.com/ to test, visualize and understand what your regular expression is doing.

Otherwise for your problem, you just had to avoid the "special characters" to be interpreted by your shell; in my examples I put them between single quotes but you could have escaped them (ie: tr -d \[\" is equivalent to tr -d '["').

Cœur
  • 37,241
  • 25
  • 195
  • 267
bufh
  • 3,153
  • 31
  • 36
  • Thank you for helping me out. It solves my problem, but it replaces all [" with ".". I very specifically like to replace fw[" with fw. and leave the rest as it is – Bala Apr 11 '16 at 13:09
  • Edited... does it fit your need now? – bufh Apr 11 '16 at 13:16
  • Indeed. I got what I need. Thank you for quicker and best response. – Bala Apr 11 '16 at 13:20