Search code examples
stringparsingawkkshnawk

Search for a number in each line of a string in ksh and nawk


The input file:

vnic10
e1000g1
e1000g2
vnic10
blablabla888blablablabla999blabla

Output needed:(Only the numbers in each line)

10
1000 1
1000 2
10
888 999

We can do this using sed and remembered patterns. I am looking for the logic to get this done using awk/nawk and ksh commands.


Solution

  • Not the best formatting, but tr does the job

    $ tr '[a-z]' ' ' < file_containing_input
        10
     1000 1
     1000 2
        10
             888            999 
    

    Using awk:

    $ awk '{ gsub(/[a-z]+/, " "); print }' file_containing_input
     10
     1000 1
     1000 2
     10
     888 999 
    

    And one in bash (now I need to stop...)

    $ while read a; do echo ${a//[a-z]/ }; done < file_containing_input
    10
    1000 1
    1000 2
    10
    888 999