0

Suppose I have table like:

This is first line
name                                minimum     maximum     config_value 
----------------------------------- ----------- ----------- ------------ 
some text goes here for sample                0           1            0 
Output Ends

The out should be like column 2nd:

minimum    
----------- 
          0

What will be the awk command to generate the output like this?

  • "This is first line" and "Output Ends" are in the table? – yeti Apr 7 '15 at 7:59
  • "This is first line" and "Output Ends" should not be visible in output. – snoop Apr 7 '15 at 8:07
1

If awk is not an substantial requirement, you could cut the file in pieces on fixed bytes (and grep away empty lines):

cut -b 37-47 data | grep -v '^$'
  • and grep away those first and last marker lines – Skaperen Apr 7 '15 at 8:27
  • Be careful that length of first column may vary depending on the "name" value. – apaul Apr 7 '15 at 8:30
  • And start byte and length might vary. Well, this is more an example for using cut to achieve what OP is asking for, based on the information the question offers, than an final solution. – FloHimself Apr 7 '15 at 8:34
1

I'm no awk guru, but if your output is always the same (I mean, second line is headers, and third line contains results), you can use something like this:

cat /tmp/test.txt | awk 'NR==2 {headers=$0; start=0; len=0} NR==3 { start=index($0," "); len=length($2)+1; print substr(headers,start,len) ; print substr($0,start,len)} NR==4 {print substr($0,start,len)}'

Since it's length delimited fields, you can get the starting index and length of your field by reading the "underline" line (NR==3).

If your SQL returns more than one line, so you'd have to use something like if( NR > 1 && NR <= x ).

  • Can we have an example which does not contain , in awk command? as I have to write this command in csv file. – snoop Apr 7 '15 at 8:14
  • @snoop : sorry, I'm not able to make it without comma. Can't you define another field separator for your CSV file? Otherway, are you really required to use awk only as @FloHimself specified? – apaul Apr 7 '15 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.