How to find a line that contains numerical values?

i.e. I want to find some line that has some digits in it. I am using Ubuntu 16.04 . Can I do this with the grep command ?


Solution 1:

Yes you can!

grep '[0-9]' file

Replace file with the name of your file...

Solution 2:

Here are a few choices, all using the following test input file:

foo
bar 12
baz

All of these commands will print any input lines containing at least one number:

$ grep '[0-9]' file
bar 12
$ grep -P '\d' file
bar 12
$ awk '/[0-9]/' file
bar 12
$ sed -n '/[0-9]/p' file
bar 12
$ perl -ne 'print if /\d/' file
bar 12
$ while read line; do [[ $line =~ [0-9] ]] && printf '%s\n' "$line"; done < file
bar 12
$ while read line; do [[ $line = *[0-9]* ]] && printf '%s\n' "$line"; done < file
bar 12