Having GNU awk
you can use the following command:
awk 'NF {print $1}' FPAT='\\y[[:digit:]]+\\y' file
The key here is the use of the FPAT
variable which is a GNU extension. FPAT
stands for field pattern and describes what is a field. In our case we want a field to be a number "enclosed" within word boundaries (\y
, needs to get doubly escaped because it appears in a shell string).
NF {print $1}
checks if the first field (number) exists; in that case the number of fields (NF
) is greater than zero. If that's the case the first field will get printed.
Btw, probably your sed
is able to do this?
echo "hello12 54 world23 43 " \
| sed 's/\(\b\|^\)\([0-9]\{1,\}\)\(\b\|$\)/\n\2\n/' \
| sed '/^[0-9]\{1,\}$/!d' \
| sed '1!d'
Sorry I can only guess if you can't say the exact sed
version.
The first sed
command extracts numbers that stands alone on separate lines. The second sed
command deletes all lines which do not consist of a number only and the last one deletes everything except of the first line - if it exists.