Imagine you have the following 2 input files with numbers on the same line or on different lines as shown hereunder:
INPUT:
more digits*
::::::::::::::
digits2.in
::::::::::::::
100
10
20
35
67
8
::::::::::::::
digits.in
::::::::::::::
100,10,20,35,67,8
You can run the following grep
command to fetch only the single digit (this work for both files):
$ grep -o '\b[0-9]\b' digits.in
8
$ grep -o '\b[0-9]\b' digits2.in
8
Explanations:
The regex \b[0-9]\b
will match a single digit surrounded by word boundary character, the -o
option is used to print only that result and not the whole line as the default behavior does.
In case there are several numbers composed of a single digit:
INPUT2:
more digits*
::::::::::::::
digits2.in
::::::::::::::
100
10
20
35
67
8
9
::::::::::::::
digits.in
::::::::::::::
100,10,20,35,67,8,9
OUTPUT:
$ grep -o '\b[0-9]\b' digits2.in
8
9
$ grep -o '\b[0-9]\b' digits.in
8
9
This will output all the numbers composed of a single digit.