2

My command's output is something like:

                                   N O D E   O U T P U T

THE FOLLOWING TABLE IS PRINTED FOR NODES BELONGING TO NODE SET NSET_OUTPUT

   NODE FOOT-   NT11     
        NOTE

      3       20.00     
     11       20.00     
   1003       23.75     
   1011       23.75     
   2003       27.52     
   2011       27.52     
   3003       31.30     
   3011       31.30     
   4003       35.08     
   4011       35.08     
   5003       38.87     
   5011       38.87     
   6003       42.67     
   6011       42.67     
   7003       46.48     
   7011       46.48     
   8003       50.29     
   8011       50.29     
   9003       54.12     
   9011       54.12     
  10003       57.95     
  10011       57.95     
  11003       61.79     
  11011       61.79     
  12003       65.64     
  12011       65.64     
  13003       69.50     
  13011       69.50     
  14003       73.37     
  14011       73.37     
  15003       77.25     
  15011       77.25     
  16003       81.14

The first column is always a number,My purpose is to get the second column only, like:

20.00
20.00
23.75
23.75
27.52
27.52
31.30
31.30
.
.

I intended to use

# gawk -f nset_output.awk electric_thermal.dat
BEGIN{
}
{
               if($12~/NSET_OUTPUT/ ){
                   for(i=1;i <= 5; i++){
                       getline
                   }
                   x=$2

        print x >"nset_output.dat"

    }       

}

to accomplish this.but,the output is messed up: (only one value 20.00) 20.00

So,How do I get the second column's value? (in my case I have 405 rows corresponding 405 values with 1 column)

  • If the header is fixed-length, you could remove it with just tail, and then awk for the other column. I'd suggest taking some care in indenting code, the snippet in the question is quite hard to interpret since the indentation and the braces do not match. – ilkkachu Aug 3 '16 at 14:24
1

Assuming it's enough to check for a digit in the first column:

awk '$1 ~ /[0-9]/ { print $2 }' data.in >data.out

To ensure that this only gets applied to lines after the NSET_OUTPUT line, you could do something like the following:

sed '1,/NSET_OUTPUT/d' data.in | awk '$1 ~ /[0-9]/ { print $2 }' >data.out

This will delete lines before NSET_OUTPUT and send the rest to the awk script.


Your script will only output one number since it, for every line of input, will look for NSET_OUTPUT, and if found, skip five lines before fetching the number from the second column.

The following is a fixed version of your script:

BEGIN { print_values = 0 }

$12 ~ /NSET_OUTPUT/ {
    for (i = 1; i <= 5; i++) {
        getline;
    }

    print_values = 1;
}

print_values == 1 {
    x = $2;
    print x >"nset_output.dat"
}
  • Keeping in mind that the command output has a header(see the question revision) which can contain stuff like 3G your regex need to be stricter to filter such values – sjsam Aug 3 '16 at 13:35
  • @sjsam Not quite sure I see that myself (looking through the revisions), but I've updated the answer nonetheless (in a way that makes it different from yours and Stephen's). – Kusalananda Aug 3 '16 at 13:44
0

We can test to see if the first field starts with a number and just print the second

awk '$1 ~ /^[0-9][0-9]*$/ { print $2}' electric_thermal.dat > nset_output.dat

This matches your source file and returns

20.00
20.00
23.75
23.75
27.52
27.52
31.30
31.30
....
  • Why not [0-9]+ coz first column is always a number ? Also clearly the input comes from a command, so it should be piped – sjsam Aug 3 '16 at 13:36
  • Not all versions of awk support the + syntax. The question said that data came from electric_thermal.dat – Stephen Harris Aug 3 '16 at 13:48
  • Hmm,,, I overlooked the comment in the script. Thanks. Regarding + I never had a problem using with them with gnu/non-gnu awks What version did you mean? – sjsam Aug 3 '16 at 14:05
  • Old old systems :-) – Stephen Harris Aug 3 '16 at 14:10
0

awk is your friend:

awk '$1 ~ /^[[:digit:]]+$/{print $2}' electric_thermal.dat >outfile

should do it

0

Command

 awk '$1 ~ /^[0-9]*$/{print $2}' file name


output
20.00
20.00
23.75
23.75
27.52
27.52
31.30
31.30
35.08
35.08
38.87
38.87
42.67
42.67
46.48
46.48
50.29
50.29
54.12
54.12
57.95
57.95
61.79
61.79
65.64
65.64
69.50
69.50
73.37
73.37
77.25
77.25
81.14
-1

If the above 7 lines are persistent after executing the command every time, use:

yourcommand | sed 1,7d | awk '{print $2}'

sed 1,7d will simply hide the first 7 lines, which you don't require. awk will sort out the exact content you are actually looking for...

cat yourcommand_out | sed 1,7d | awk '{print $2}'
20.00
20.00
23.75
23.75
27.52
27.52
31.30
31.30
35.08
35.08
38.87
38.87
42.67
42.67
46.48
46.48
50.29
50.29
54.12
54.12
57.95
57.95
61.79
61.79
65.64
65.64
69.50
69.50
73.37
73.37
77.25
77.25
81.14
  • 1
    Downvote: Useless [ use of cat ], and the solution doesn't satisfy the requirements. Also please format the code. – sjsam Aug 3 '16 at 14:33
-2

Try this:

awk '/^[ ]*[[:digit:]]/ {print $2}' inputFile
  • 1
    Please learn to use code blocks. – HalosGhost Aug 3 '16 at 17:16

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.