I wanna replace “output_cap=013”, “output_cap=55”, “output_cap=3757” with “output_cap=1” in many files. How do I do it using sed?
I tried
sed -i 's/output_cap=???/output_cap=1/g' xxx_555_fff.dat
But now working?
I wanna replace “output_cap=013”, “output_cap=55”, “output_cap=3757” with “output_cap=1” in many files. How do I do it using sed?
I tried
sed -i 's/output_cap=???/output_cap=1/g' xxx_555_fff.dat
But now working?
Try sed regex that matches digits. Typically we use
[0-9]
The above will match one digit. To check more than one digit, try:
[0-9]+
The syntax is
# just see changes on the screen
sed 's/output_cap=[0-9]\+/output_cap=1/g' xxx_555_fff.dat
# update the file
sed -i 's/output_cap=[0-9]\+/output_cap=1/g' xxx_555_fff.dat
In short this is what you need
[0-9]
- Match one digit.[0-9]*
- Match zero or more digits.[0-9]\+
- Match one or more digits.[0-9]\{1,\}
- Match one or more digits.output_cap=[0-9]\+$
- Match output_cap=013
and but now output_cap=3757 xxx
.Try using bash for loop or bash while loop. For example:
for f in *.dat
do
# update files in place and make a backup with .bak file in the current dir
sed -i'.bak' 's/output_cap=[0-9]\+/output_cap=1/g' "${f}"
done
See the https://www.cyberciti.biz/faq/how-to-use-sed-to-find-and-replace-text-in-files-in-linux-unix-shell/ and Regular Expressions - sed, a stream editor for more info.