awkgrepkshaix

Extract substring from a field with single AWK in AIX


I have a file, file, with content like:

stringa    8.0.1.2     stringx
stringb    12.01.0.0    stringx

I have to get a substring from field 2 (the first two values with the dot). I am currently doing cat file | awk '{print $2}' | awk -F. '{print $1"."$2}' and am getting the expected output:

8.0
12.01

How can I do this with a single AWK?

I have tried with match(), but I am not seeing an option for a back reference.


Solution

  • You can do something like this.

    awk '{ split($2,str,"."); print str[1]"."str[2] }' file
    8.0
    12.01
    

    Also, keep in mind that your cat is not needed. Simply give the file directly to awk.