简体   繁体   中英

regex replace in linux for multiple files

I have 200k files in a single folder in linux server where i need to transform this files using regex here is the regex to find in text file

([^\s]+?.*)=((.*(?=,$))+|.*).*

now I need to replace it with below substitution value

"$1":"$2",

the above regex is working fine when i used them in python. the server which i am working does not support python, so i need to use bash commands. i have tried below bash command but it is not working

command:

sed -r  's/([^\s]+?.*)=((.*(?=,$))+|.*).*/"$1":"$2",/g' *20200502* 

the above bash command is not working

Fixed your regex:

sed -E 's/([^[:space:]]+?.*)=((.*(=?,$))+|.*).*/"\1":"\2",/g' *20200502*
  1. Replaced inappropriate [^\\s] by its POSIX ERE syntax equivalent [^[:space:]] .
  2. Fixed the misplaced optional marker ?=,$ with =?,$ instead.
  3. Fixed the invalid capture group reference syntax "$1":"$2" with "\\1":"\\2" .

Kinda difficult to test but just analyzing your approach this might work:

sed -i -E "s/([^\s]+?.*)=((.*(?=,$))+|.*).*/\1$1\2$2/" *20200502* 
  • \\1 and \\2 in the second part are references to the groups captured in the first part
  • -E for extended regular expressions (+ and grouping)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM