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
Related videos on Youtube
Author by
user2861089
Updated on September 18, 2022Comments
-
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 almost 6 yearsthanks, 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 almost 6 yearsthanks, 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 almost 6 years@user2861089 in that case I would choose one of the
sed
variants (although recent versions of GNU awk akagawk
can also do in-place edits) -
muru almost 6 years@user2861089 "Use
sed -i
(...) for in-place editing of the file."