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?
to avoid issues due to input file names, use grep -rlZ 'regex' | xargs -0 <command ...>
if content within parenthesis doesn't have ))
, then you can use this
grep -rlZ 'print((' | xargs -0 perl -i -0777 -pe 's/print\(\((.*?)\)\)/print($1)/sg'
-0777
to slurp entire file content as a single string, so this solution not fit for large files that cannot fit memory requirements.*?
is non-greedy matchings
modifier allows to match \n
as well for .
When using -i
option, you can specify a backup suffix (ex: -i.bkp
) or prefix (ex: -i'bkp.*'
) or even a backup directory (ex: -i'bkp_dir/*'
) - these help in preserving the original copy for further use