1

I have a large text file code.txt with the following equalities

        oReservist.Role = Sheets("RECAP").Cells(ligne, 2)
        oReservist.Surname = Sheets("RECAP").Cells(ligne, 3)
        oReservist.Name = Sheets("RECAP").Cells(ligne, 4)
        ...

I want to change the first part of the equality by the second and the second by the first one. That is to say :

        Sheets("RECAP").Cells(ligne, 2) = oReservist.Role
        Sheets("RECAP").Cells(ligne, 3) = oReservist.Surname
        Sheets("RECAP").Cells(ligne, 4) = oReservist.Name 
        ...

How can I handle that ? Until now I can only delete the parts before and after :

sed 's/.*=.*//' file

closed as off-topic by Jeff Schaller Jun 25 at 18:52

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question has been posted on multiple sites. Cross-posting is strongly discouraged; see the help center and community FAQ for more information." – Jeff Schaller

  • 1
    Also asked on superuser. Please don't post a question on multiple sites. You waste people's time when they answer here but there's more appropriate answers elsewhere. – glenn jackman Jun 24 at 14:32
4

Using awk with " = " as field separator, and simply turn around the fields.

awk -F " = " '{OFS=FS;print $2,$1}' file

or using sed:

sed 's/\(.*\) = \(.*\)/\2 = \1/' file

(using capture groups \(...\) and calling them in the replacement string with \1 and \2)

Both will not eliminate the tab or white space at the beginning of the line.

Output:

Sheets("RECAP").Cells(ligne, 2) =         oReservist.Role
Sheets("RECAP").Cells(ligne, 3) =         oReservist.Surname
Sheets("RECAP").Cells(ligne, 4) =         oReservist.Name

To keep them, you can capture them somehow, e.g.:

sed 's/\([[:blank:]]*\)\(.*\) = \(.*\)/\1\3 = \2/' file

or leave out the blanks from the first capture:

sed 's/\([^[:blank:]]*\) = \(.*\)/\2 = \1/' file

Output:

        Sheets("RECAP").Cells(ligne, 2) = oReservist.Role
        Sheets("RECAP").Cells(ligne, 3) = oReservist.Surname
        Sheets("RECAP").Cells(ligne, 4) = oReservist.Name
  • Thank uou, that makes sense ! I tried it on my 2009 Windows powershell. Yet it seems that sed is not recognised :'( – ThePassenger Jun 24 at 13:22
  • That means you need to edit your tags to get more appropriate eyes on your problem. Add the windows and powershell tags, and remove sed – glenn jackman Jun 24 at 14:28
  • @pLumo, you can simplify your sed a bit: 's/([^[:blank:]]+) = (.*)/\2 = \1/' – glenn jackman Jun 24 at 14:29
  • sure, thanks for suggestion, added! – pLumo Jun 24 at 14:31
0

Tried with below 2 methods

python

#!/usr/bin/python
m=open('filename','r')
for i in m:
    k=i.strip().split('=')
    fi=k[1]
        la=k[0]
        print "{0}={1}".format(fi,la)

output

Sheets("RECAP").Cells(ligne, 2)=oReservist.Role 
 Sheets("RECAP").Cells(ligne, 3)=oReservist.Surname 
 Sheets("RECAP").Cells(ligne, 4)=oReservist.Name 

Method2

awk -F "=" '{print $2"="$1}' y.txt|sed -r "s/\s+//g" 

Sheets("RECAP").Cells(ligne,2)=oReservist.Role
Sheets("RECAP").Cells(ligne,3)=oReservist.Surname
Sheets("RECAP").Cells(ligne,4)=oReservist.Name

praveen@praveen:~$

Not the answer you're looking for? Browse other questions tagged or ask your own question.