bash - Extract data from a subsection of a file using Shell commands or Perl -


i'm trying extract information particular section of file (under point). more precisely, want able keyword, example "m2", , find "freq" under m2 number without mixing m1 or m3.

most easiest answer combination of grep, sed, awk or cut.

m1{

freq: 1ghz

temp: 125c

}

m2{

freq: 1.1ghz

temp: 130c

}

m3{

freq: 0.78ghz

temp: 89c

}

using perl one-liner

perl -ne 'print $1 if /m2\{/ .. /\}/ , /freq:\s*(.*)/' file 

explanation:

switches:

  • -n: creates while(<>){..} loop each “line” in input file.
  • -e: tells perl execute code on command line.

code:

  • /m2\{/ .. /\}/: range operator test between 2 lines
  • /freq:\s*(.*)/: find matching line, , capture value in $1.

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -