1

I have a file with a wide range of information and I want to extract some data from here. I only will post here the interesting part. I want to extract IQ and JQ values as well as the J_ij[meV] value which is two lines above. I read this question How to print 5 consecutive lines after a pattern in file using awk where a pattern is used to extract information bellow and I was thinking doing something similar. My initial idea was:

awk '/IQ =/ { print $6,$12 }  /IQ =/ {for(i=2; i<=2; i++){ getline; print $11 }}' input.text > output.txt

Loop appears not to working

IT   IQ   JT    JQ   N1 N2 N3    DRX    DRY    DRZ     DR         J_ij [mRy]     J_ij [meV]

 IT =  1 IQ =  **1**                     JT =  1 JQ =  **1**

->Q = ( -0.250,  0.722,  0.203)   ->Q = ( -0.250,  0.722,  0.203)

1    1    1    1    0  0  0   0.000  0.000  0.000   0.000      0.000000000     **0.000000000**

 IT =  1 IQ =  **1**                     JT =  6 JQ =  **6**

 ->Q = ( -0.250,  0.722,  0.203)   ->Q = (  0.000,  1.443,  0.609)

  1    1    6    6   -1  0 -1  -0.250 -0.144 -0.406   0.498      0.135692822     **1.846194885**

 IT =  1 IQ =  **1**                     JT =  8 JQ =  **8**

  ->Q = ( -0.250,  0.722,  0.203)   ->Q = (  0.000,  0.577,  0.609)

  1    1    8    8    0  0 -1   0.250 -0.144 -0.406   0.498      0.017676555     **0.240501782**

My expected output is:

IQ JQ J_ij [meV]
1 1 0.000000000
1 6 1.846194885
1 8 0.240501782

It comes from the bold words (** **), first line is only indicative.

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • 1
    Welcome to SO, special thanks for adding your attempted code in your question. Your sample of input is NOT clear, so please do wrap it in CODE TAGS, also do mention expected output in CODE TAGS in question and let us know then. – RavinderSingh13 Jan 22 '20 at 11:59
  • 1
    `I have a syntax error on "&&" and I don't know how I can fix it` - I don't know what that `&&` should mean, but it looks like you want to just remove the `&&`. Or you could replace the whole `} && /IQ =/ {` by just `;` – KamilCuk Jan 22 '20 at 12:02
  • @KamilCuk it solved half of my problem, now I need to extract information from 2 lines below my tag (loop appears not to working) – Miguel Cardoso Jan 22 '20 at 12:05
  • Well, look at `i=2; i<=2` You want to extract information from 2 lines below your tag or from the _second_ line below your tag? It looks like `print $6, $12; getline; getline; print $13`. Sure. Can you explain how the output is created? Where does the first column come from? The second column (there are many 6s in the input)? The third column? – KamilCuk Jan 22 '20 at 12:07
  • I only want to extract from these line, I don't know how I can do it @KamilCuk. I want to extract from the second line below my tag. I reedited my question. – Miguel Cardoso Jan 22 '20 at 12:08
  • @MiguelCardoso, Could you please be more clear in your sample of input, as it is not clear. What are the conditions of getting data? Is it like each line which has a `IQ =` other values are present after 2 lines of it? Kindly do add more details on same. – RavinderSingh13 Jan 22 '20 at 12:18
  • @RavinderSingh13 The output text is generated automatically by another program, the file is always the same. As you said it has an IQ on one line and after 2 lines there is some numbers I want to extract, I can upload the output but It will be even harder to understand what is going on there. – Miguel Cardoso Jan 22 '20 at 12:25

1 Answers1

1

Could you please try following. Written and tested with shown examples.

awk '
BEGIN{
  print "IQ JQ J_ij [meV]"
}
FNR>1 && /IQ =/{
  value=$6 OFS $12
  found=1
  next
}
found && NF && !/ ->Q/{
  if(value){
     print value OFS $NF
  }
  value=found=""
}'  Input_file

Output will be as follows.

IQ JQ J_ij [meV]
1 1 0.000000000
1 6 1.846194885
1 8 0.240501782
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93