Bala
Bala

Reputation: 11

Find and replace - bash

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?

Upvotes: 0

Views: 65

Answers (1)

bufh
bufh

Reputation: 3410

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 '["').

Upvotes: 1

Related Questions