mardi 8 mai 2018

Sed REMOVE / REPLACE double parentheses

I have a python file, which has lots of double parentheses like this, which i would like to replace with single parenthesis. Sometimes the print goes on for 2 lines or more.

print(('>>   # some text some text some text and ' +
                                                    'some more text'))
print(('>>   # some text some text some text and ' +
                                                    'some more text'))

print(('>>   # some text some text some text and ' +
                                                    'some more text'))


print(('>>   # some text some text some text and ' +
                                                    'some more text'))


print((something))
print((something))

print((something))

print((something))


print((something))



print((something))

I have tried a lot different ways to approach this. I think the easiest would be with sed. I have something like this:

grep -rl 'print((' test.txt | xargs sed -i "N;s/print((\(.*\)))/print(\1)/g"

The output looks like this:

print('>>   # some text some text some text and ' +
                                                    'some more text')
print('>>   # some text some text some text and ' +
                                                    'some more text')

print(('>>   # some text some text some text and ' +
                                                    'some more text'))


print(('>>   # some text some text some text and ' +
                                                    'some more text'))


print(something)
print(something)

print(something)

print(something)


print(something)



print(something)

Now with some lines it works but with some it doesn't, i think it is because of the N; but i need this in case it is multiple lines long..

What could i do to improve this pattern?

Thanks,

Sam

Aucun commentaire:

Enregistrer un commentaire