Use sed to replace string in several files or several strings in a file
08 August 2019
I used ‘sed’ not so often but each time it is a nice wonder to realize what you can do with it.
I am on Windows and I am using Gow to use such tool from linux (like grep, ls, and so on) with the cmd console.
You can do a lot of things with this tool that I will not explain (because actually I know really litle, but you can find a lot on the web).
The actual scenario I had was a logfile with several lines that I add to change.
I had to filter which line I wanna change and to just change the part I need so for each line I had a sed command like this one :
sed -i -E "s/(.+)S=2:C=4(.+)BAR CODE READ, NOCODE(.+)/\1S=2:C=4\2BAR CODE READ, AP0001\3/g" "path\of\your\file.log"
Was changing this
[2019/08/07 11:11:33] INFO: [S=2:C=4(LDV384):I=32(sbcr):Q=50:M=2] DATA, BAR CODE READ, NOCODE^M
[2019/08/07 11:11:33] INFO: [S=2:C=4(LDV384):I=32(sbcr):Q=50:M=2] DATA, BAR CODE READ, AP0001^M
I was using 1 sed cmd per substitution because I had a lot of NOCODE to substitute in the same file so I had to be sure I am picking the right line to work on.
-E is meaning that Regex Expression is used.