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


The above will match one digit. To check more than one digit, try:


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
   # 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}"

See also

See the How to use sed to find and replace text in files in Linux / Unix shell - nixCraft and Regular Expressions - sed, a stream editor for more info.