Thanos
Thanos

Reputation: 586

awk Find a pattern after a pattern and print its value

I have a file which has the following format

[/Experiment/Run Parameters/Digitizer_0/CAEN_x725_x730_Settings/Channel_1/DPP_PSD_Params]
Short_Gate = WORD : 30
Long_Gate = WORD : 100
Gate_Offset = WORD : 32

[/Experiment/Run Parameters/Digitizer_0/CAEN_x725_x730_Settings/Channel_2/DPP_PSD_Params]
Short_Gate = WORD : 300
Long_Gate = WORD : 1000
Gate_Offset = WORD : 32

[/Experiment/Run Parameters/Digitizer_1/CAEN_x725_x730_Settings/Channel_1/DPP_PSD_Params]
Short_Gate = WORD : 3000
Long_Gate = WORD : 10000
Gate_Offset = WORD : 32

[/Experiment/Run Parameters/Digitizer_1/CAEN_x725_x730_Settings/Channel_2/DPP_PSD_Params]
Short_Gate = WORD : 30000
Long_Gate = WORD : 100000
Gate_Offset = WORD : 32

What I'd like to do is get the values from some of these variables (i.e. 30 for Short_Gate, 100 for Long_Gate etc). So the steps should be

  1. Find pattern i.e. [/Experiment/Run Parameters/Digitizer_0/CAEN_x725_x730_Settings/Channel_2/DPP_PSD_Params]. There are similar patterns like this, with slight differences (i.e. Channel_3 instead of Channel_2 or Digitizer_1 instead of Digitizer_0 etc) throughout the file
  2. After that pattern, find the first instance of i.e. Short_Gate
  3. Give the value that's after WORD : or the value of the last field.

I know how to match a pattern in awk and print a number of lines after that using for instance

awk '/Parameters\/Digitizer_0\/CAEN_x725_x730_Settings\/Channel_2\/DPP_PSD_Params/ {print}' file

but how to find a pattern after a pattern and get the value after = WORD : ? I can always use -F ":" and print the second column but the format of the file can be changed. Also there are multiple entries in the file that start with Short_Gate, Long_Gate etc

The expected output should look like this

30

when [/Experiment/Run Parameters/Digitizer_0/CAEN_x725_x730_Settings/Channel_1/DPP_PSD_Params] and Short_Gate is requested,

100

when[/Experiment/Run Parameters/Digitizer_0/CAEN_x725_x730_Settings/Channel_1/DPP_PSD_Params] and Long_Gate is requested etc.

Upvotes: 0

Views: 140

Answers (3)

Ed Morton
Ed Morton

Reputation: 203532

$ awk -v hdr='[/Experiment/Run Parameters/Digitizer_0/CAEN_x725_x730_Settings/Channel_2/DPP_PSD_Params]' \
      -v fld='Short_Gate' '
    !NF { f=0 }
    f && ($1 == fld) { print $NF }
    $0 == hdr { f=1 }
' file
300

Upvotes: 1

RavinderSingh13
RavinderSingh13

Reputation: 133518

Could you please try following, based on OP's latest edited expected output.

awk -v var="/Experiment/Run Parameters/Digitizer_0/CAEN_x725_x730_Settings/Channel_1/DPP_PSD_Params" '
/^\[/{
  found=""
}
$0=="[" var "]"{
  found=1
}
found && /Short_Gate/ && match($0,/WORD : [0-9]+/){
  val=substr($0,RSTART,RLENGTH)
  gsub(/[^0-9]+/,"",val)
  print val
  val=""
}' Input_file

Upvotes: 1

anubhava
anubhava

Reputation: 785156

You may use this awk:

awk -F ' *: *' '/^\[/ {
   p = (index($0, "/Experiment/Run Parameters/Digitizer_0/CAEN_x725_x730_Settings/Channel_2/DPP_PSD_Params") > 0)
}
p && NF==2 {
   print $2
}' file

30
100
32
2162753
8711
261
65535
0
0
0
2

Upvotes: 1

Related Questions