Remove all non-numeric characters from text files

5,663

Solution 1

With sed:

sed 's/[^[:digit:]]\+//g'
  • [^[:digit:]]\+ matches one or more (+) non-digits ([^[:digit:]]) and we're replacing that with empty string, globally (g)

Use sed -i (or sed -i.bak for keeping the original with a .bak extension) for in-place editing of the file.


Same thing with awk's sub(Regex, Replacement, Input) function:

awk 'sub("[^[:digit:]]+", "", $0)'

Use --inplace for in-place editing of the file.


Example:

% cat file.txt                 
Sydney  33
Castle hill  47
Lake's town hill  79

% sed 's/[^[:digit:]]\+//g' file.txt               
33
47
79

% awk 'sub("[^[:digit:]]+", "", $0)' file.txt
33
47
79

Solution 2

With tr (and assuming you don't want to remove newlines)

$ tr -dc '[0-9\n]' < file1.txt
33
47
79

Given the structure of your file, you could also use awk to print the last whitespace-delimited field:

$ awk '{print $NF}' file1.txt
33
47
79
Share:
5,663

Related videos on Youtube

user2861089
Author by

user2861089

Updated on September 18, 2022

Comments

  • user2861089
    user2861089 over 1 year

    I want to remove all non-numeric characters from a bunch (~2000) of .txt files.

    For example, file1.txt:

    Sydney  33
    Castle hill  47
    Lake's town hill  79
    

    should become, file1.txt:

    33
    47
    79
    

    I want to change the content of each text file, not print the output on screen. Thanks!

  • user2861089
    user2861089 almost 6 years
    thanks, I like this but I don't want to print the output on screen, I want to change the content of each txt file - thanks for your help
  • user2861089
    user2861089 almost 6 years
    thanks, I like this but I don't want to print the output on screen, I want to change the content of each txt file - thanks for your help
  • steeldriver
    steeldriver almost 6 years
    @user2861089 in that case I would choose one of the sed variants (although recent versions of GNU awk aka gawk can also do in-place edits)
  • muru
    muru almost 6 years
    @user2861089 "Use sed -i (...) for in-place editing of the file."