Junaid Junaid - 8 months ago 42
Linux Question

Extracting column from a multi section file

not sure what exactly to write in title section. I have a scenario where I want to extract a specific column from multi section file. For example:

unit id ip
32 43

machine price
431-P 45k
S-34 10k

I want to extract the contents under machine column. Any idea how i can do that?


If the machine section is the last section, like in your example, then you could use awk to look for machine in the first field of the record and just print any record found after that point:

 awk '$1=="machine"{machineSection=1}machineSection==1{print $0}' test.txt

This test the first field $1 to see if it's machine. If it is then it sets the machineSection variable to 1. Then it tests to see if machineSection variable is 1 then it will print the record out.