-1

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 brings me to the the question: how does tr process \\ and ; in parameters ?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
akmot
  • 63
  • 8

2 Answers2

1

Try with sed :

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

GregThB
  • 283
  • 1
  • 7
0

Using sed

$ sed -E ':a;s~\\(.*;)~\1~;ta' input_file
WWWWWWWWWWWWWW;XXXXXXXXXXXXXXXXXXXX;ZZZZZZZZZZZZZZ;EE;/usr/lib/systemd/system/sysinit\target
HatLess
  • 10,622
  • 5
  • 14
  • 32
  • While it looks reasonable to me, the OP wants to replace semicolons **followed by a backslash**. You have removed the very first backslah in the line, although it is not preceeded by a semicolon. – user1934428 Jul 20 '22 at 09:11
  • @user1934428 That may very well be a valid point. However, he does also show the format the file currently is which does not have the preceeding semicolon. Hopefully, OP can clarify then I will adjust accordingly. Thanks – HatLess Jul 20 '22 at 09:17