sed: Remove All Digits / Input From Input

Q. How do I remove all digits or number from my text file or input using sed command?

A. You need to use regular expression to remove digits or number from input. For example, ‘s/[0-9]*//g’ will remove all numbers from the input.

ADVERTISEMENTS

Remove all digits

Type the following command:
$ echo 'This is a test 12335 and 669384 535xy4' | sed 's/[0-9]*//g'
Sample output:

This is a test  and  xy

You can remove all digits from a text file:
$ sed 's/[0-9]*//g' input.txt
$ sed 's/[0-9]*//g' input.txt > output.txt

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
6 comments… add one
  • dimka Aug 19, 2009 @ 9:46

    But how can i remove all, but digits?!

  • 🐧 nixCraft Aug 19, 2009 @ 12:04

    Try:
    echo 'This is a test 12335 and 669384 535xy4' | sed 's/[^0-9]*//g'

  • dimka Aug 21, 2009 @ 6:04

    Oh, thanks! That’s what I was looking for!

  • np Sep 5, 2012 @ 14:56

    Hi!
    I’m doing this:

    echo 'something=1234
    something=12345
    andsomething=1234&somethingelse&something=1234
    something=2345' | sed 's/something=1234[^0-9]/something=9999/g'
    

    The output is :

    something=1234
    something=12345
    andsomething=9999&somethingelse&something=1234
    something=2345

    And what I’m trying to get is :

    something=9999
    something=12345
    andsomething=9999&somethingelse&something=9999
    something=2345

    Why isn’t it working when the pattern is at the end of the line ? What a pain….
    Help, anyone ?

  • wudeng Dec 11, 2013 @ 9:17

    you can use \b instead of [^0-9], \b means word boundary in regexp

  • Vlad May 23, 2016 @ 14:15

    sed -i “s/…/…/g” file
    replaces the contents of file

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.