To remove all characters from input except digits (numbers) using sed
, you can use the following command:
sed 's/[^0-9]//g'
This command will search the input for any characters that are not in the range 0-9, and delete them. The g
flag specifies that the substitution should be performed globally, on all occurrences of the pattern.
For example, if you have the following input:
This is a test 123456.
Running the sed
command above will output:
123456
You can also use the [:digit:]
character class to remove all characters except digits from the input:
sed 's/[^[:digit:]]//g'
This will have the same effect as the first command, and remove all characters except digits from the input.
By using the [^0-9]
pattern or the [^[:digit:]]
character class in a sed
substitution, you can remove all characters except digits from input. This can be useful for processing text data or for removing non-numeric characters from a script.