0

How can I write a fast shell script to match the following in linux. I have tried various combinations of grep commands, sed commands but no success. The "message" section can be repeated any number of times. The contents of the "message" itself will be generic but fixed to the format shown below. I want the output to be something like:

x: y
x1: y1
x: y
x1: y1
x: y
x1: y1

Input:

Posting to abcd
message {
 a {
     x : y
     x1 : y1
   }
}
message {
 a {
     x : y
     x1 : y1
   }
}
message {
 a {
     x : y
     x1 : y1
   }
}
Arjun
  • 385
  • 5
  • 17
  • Check out pcregrep as per [this](http://stackoverflow.com/a/2686705/2413778) answer. – John C Jun 30 '14 at 11:47
  • what to match exactly a section starting with Posting followed by 3 message section with a single section of a that contain exactly x : y and x1 : y1 value ? or a bit more generic with more or less section/sub section and variable name + value each of different content like string/number/other ? And this in a huge file with several occurence ? – NeronLeVelu Jun 30 '14 at 12:33
  • Ya the "message" section can be repeated any number of times. The contents of the message itself will be generic but fixed to the format shown above. – Arjun Jun 30 '14 at 12:57
  • possible duplicate of [Parsing json with sed and awk](http://stackoverflow.com/questions/1955505/parsing-json-with-sed-and-awk) – tripleee Jun 30 '14 at 13:14
  • Can you give a better description of what you want, this command `awk '/^ /' inputfile` will give the output you suggested given the input file but im almost sure it wont work for what you actually want. –  Jun 30 '14 at 14:23
  • As I understand it, I would propose `sed -n 's/^ *\([^ :]\+\) *: *\([^ :]\+\)/\1: \2/p' inputfile` but indeed the question isn't clear. Moreover the user may also want to validate the input file and output an error if the format is not recognized. – vinc17 Jul 01 '14 at 16:13

1 Answers1

1

This might work for you (GNU sed):

sed '/^message {/,/^}/{/[{}]/d;s/^\s*//}' file

Within the block message { to }, delete any lines containing a { or a } and then remove any white space from the start of any subsequent line.

potong
  • 55,640
  • 6
  • 51
  • 83