How to replace digits with sed command in Linux

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]+

Sed replacing digits command

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

How to replace digits with sed regex command

In short this is what you need

  1. [0-9] - Match one digit.
  2. [0-9]* - Match zero or more digits.
  3. [0-9]\+ - Match one or more digits.
  4. [0-9]\{1,\} - Match one or more digits.
  5. output_cap=[0-9]\+$ - Match output_cap=013 and but now output_cap=3757 xxx.

Replacing digits using sed with multiple files

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 also

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.