Home > Back-end >  Replace a semicolon followed by a backslash by just a semicolon and dont apply it to other baskslash
Replace a semicolon followed by a backslash by just a semicolon and dont apply it to other baskslash

Time:07-20

I have a file that contains data that looks like this :

WWWWWWWWWWWWWW;XXXXXXXXXXXXXXXXXXXX;ZZZZZZZZZZZZZZ;EE;/usr/lib/systemd/system/snapd.socket
WWWWWWWWWWWWWW;XXXXXXXXXXXXXXXXXXXX;ZZZZZZZZZZZZZZ;EE;/usr/lib/systemd/system/sysinit.target
WWWWWWWWWWWWWW;XXXXXXXXXXXXXXXXXXXX;ZZZZZZZZZZZZZZ;EE;/usr/lib/systemd/system/sysinit\target

Sometimes errors appear in this file, they look like this ;

\WWWWWWWWWWWWWW;\XXXXXXXXXXXXXXXXXXXX;\ZZZZZZZZZZZZZZ;EE;/usr/lib/systemd/system/sysinit\target

As you can see ther is backslashes that makes my file very hard to parse after.

I tried to make them disappeard with :

cat ligne.txt | tr "\\" "\0"

But it affects the file names containing \ and I want to keep the file name the same.

So I tried to use the condition only the \ with a semicolon next to it.

cat ligne.txt | tr ";\\" "\0"

but it gives me this :

WWWWWWWWWWWWWWXXXXXXXXXXXXXXXXXXXXZZZZZZZZZZZZZZEE/usr/lib/systemd/system/sysinittarget

The data is destroyed.

So i tried this :

cat ligne.txt | tr ";\\" ";"

but it does this witch is worst :

\WWWWWWWWWWWWWW;;XXXXXXXXXXXXXXXXXXXX;;ZZZZZZZZZZZZZZ;;EE;;/usr/lib/systemd/system/sysinit;target

This is bringing the question : How does tr process \\ and ; in parrameters ?

CodePudding user response:

Try with sed :

cat ligne.txt | sed -e 's/;\\/;/g'

CodePudding user response:

Using sed

$ sed -E ':a;s~\\(.*;)~\1~;ta' input_file
WWWWWWWWWWWWWW;XXXXXXXXXXXXXXXXXXXX;ZZZZZZZZZZZZZZ;EE;/usr/lib/systemd/system/sysinit\target
  • Related