1

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!

2

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
  • 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 Jul 18 '18 at 1:28
  • 1
    @user2861089 "Use sed -i (...) for in-place editing of the file." – muru Jul 18 '18 at 3:56
0

Use:

$ echo "Jim 5" > file.txt
$ echo "Jane 3" >> file.txt
$ sed -i 's/[^0-9]//g' file.txt
$ cat file.txt
5
3

Using your test data:

$ cat file1.txt
Sydney 33
Castle hill 47
Lake's town hill 79

$ sed -i 's/[^0-9]//g' file1.txt

$ cat file1.txt
33
47
79
0

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
  • 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 Jul 18 '18 at 1:31
  • @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) – steeldriver Jul 18 '18 at 1:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.