Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 (first two values with the dot).
I am currently doing cat file | awk '{print $2}' | awk -F. '{print $1"."$2}' and getting the expected output:

8.0
12.01

The query is how to do this with single awk?
I have tried with match() but not seeing an option for a back reference.
Any help would be appreciated.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading