-1

I have a file where I want to change values /1 to /2, and /2 to /1

I have tried

cat file.txt | tr /1 /2 > out.txt

Problem is this simply leaves me with all /2, and if I try to do:

cat file.txt | tr /1 /2 | tr /2 /1 > out.txt

then I get all /1's. How can I swap the two numbers at the same time?

I have also just tried this but no luck

sed -i -e 's/\/1/\/2/g' -e 's/\/2/\/1/g' file.txt
SaltedPork
  • 345
  • 3
  • 16

1 Answers1

0

Like this:

perl -pe 's|/(\d+)|$1 == 1 ? "/2" : "/1"|ge' file
Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223