1

data-file content:

             k = 0.1667 0.0962 0.0000 (  4922 PWs)   bands (ev):

-7.1689  -7.1043  -6.1682  -6.0658   1.1309   1.5196   3.0894   3.1087
 3.3846   3.7588   4.2143   4.2220   4.3882   4.4011   5.1222   5.6229
 5.9506   6.2078   8.4442   8.8100   8.8150   8.8529   8.9070   9.0070
 9.5911   9.6311  13.5626  14.3720  15.0169  15.2435 
  end1
  end2
  end3

I have tried two awk commands on this data...

1.

awk -F'=' '/[[:blank:]]k/{w=w""$2}; { print w}' data-file

Output:

     0.1667 0.0962 0.0000             matrix :
     0.1667 0.0962 0.0000             matrix :
     0.1667 0.0962 0.0000             matrix :
     0.1667 0.0962 0.0000             matrix :
     0.1667 0.0962 0.0000             matrix :
     0.1667 0.0962 0.0000             matrix :
     0.1667 0.0962 0.0000             matrix :
     0.1667 0.0962 0.0000             matrix :
     0.1667 0.0962 0.0000             matrix :

2.

awk -F'=' '/[[:blank:]]k/{w=w""$2}; { print w;w=""}' data-file

Output:

0.1667 0.0962 0.0000 (  4922 PWs)   bands (ev):











(Eight newlines are printed after that.)

With the second command the searched pattern is printed only once, but with more empty lines. But the first command the pattern is printed nine times. What is the difference between the awk commands: why did it print more times, and why the empty line are printed? How to suppress printing these empty lines?

  • 3
    Well, the main difference between the two scripts is this: w="". Can you guess what it means? – Michael Vehrs Jun 9 '16 at 12:53
  • 1
    Hi michael, awk does searching upto end of file. #of line is 9. at 1st line awk finds pattern and prints it. but when it goes to 2nd 3rd.. lines once agin it prints pattern. so in adding w="" deletes content in w. but in second command it prints empty line i want to suppress it how to do it – Muthu Valli Nayagam Jun 9 '16 at 13:34
  • 1
    this is problem with posting i tried to bold the commands but nit successful my commands are same thing as in the post but without ** at the end . also that the datafile contains data end1 end2 end3 statements in successive lines, these also not printed in post – Muthu Valli Nayagam Jun 9 '16 at 14:01
  • 1
    I'm confused about what you are trying to achieve: what is your actual input data and desired output? I don't see 4922 PWs or bands (ev) anywhere in your original snippet. If you want help, it's best to ask direct - instead of asking an elliptical question about why one command produces a different output from another command when neither command (apparently) produces what you want – steeldriver Jun 9 '16 at 14:04
  • 1
    print w, where w is "", is the same as print "", which prints an empty line. If you don't want to print anything, you can use an if statement so that print will only be executed under whatever conditions you choose. – Mark Plotnick Jun 9 '16 at 14:09
1

why did it print more times

Your awk script has an action with no pattern, which means that it will be carried out for each input line. If you don't want to print the pattern you search for several times, then don't do that.

why the empty line are printed

Because you print w and then set w="". Then you print w again for each input line.

The obfuscated question makes it hard to tell, but I guess you want to do this:

awk -F'=' '/[[:blank:]]k/{ print $2 }' data-file
  • yes i understand that. i want to know how to stop printing newlines . what commend i have to add in my awk – Muthu Valli Nayagam Jun 9 '16 at 14:15
  • @MuthuValliNayagam If you don't want the empty lines, please edit the question to mention that. – JigglyNaga Jun 9 '16 at 14:22
  • hi jigglynaga i have changed my question – Muthu Valli Nayagam Jun 9 '16 at 14:40
0

Those lines are printed because {print w;w=""} happens every time, even if there is nothing in w. If you only want to print the non-empty lines, you will have to make that {print...} action conditional on w having some content.

Conveniently, awk considers the empty string to be false and all non-empty strings to be true ("True and False in awk"). So you can get what you need by adding w as the pattern:

awk -F'=' '/[[:blank:]]k/{w=w""$2}; w { print w;w=""}'

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.