-1

I am trying to extract somne blocks from a text, however, it does not work now.

For example: I have a file named test01.txt with below contents:

10:10:12 ::  blah blah blah
10:10:12 ::  blah blah blah
10:10:14 ::  $VAR1 = {
10:10:14 ::     'Id' = 'A0002'
10:10:15 ::     'Name' => 'Jane'
10:10:15 ::     'Age' => '18'
10:10:15 ::  };
10:10:15 ::  blah blah blah
10:10:16 ::  blah blah blah
10:10:16 ::  $VAR1 = {
10:10:16 ::     'Id' = 'A0003'
10:10:16 ::     'Name' => 'Adams'
10:10:16 ::     'Age' => '25'
10:10:16 ::  };

expect results

{
    10:10:14 ::     'Id' = 'A0002'
    10:10:15 ::     'Name' => 'Jane'
    10:10:15 ::     'Age' => '18'
    10:10:15 ::  }
 {
    10:10:16 ::     'Id' = 'A0003'
    10:10:16 ::     'Name' => 'Adams'
    10:10:16 ::     'Age' => '25'
    10:10:16 ::  }

I have used the following command "grep -e '{|}' test01.txt", which does not solve this problem.

Any help would be really appreciated.

BirdANDBird
  • 122
  • 7
  • What is the criteria for the lines that should be returned? – Barmar Nov 10 '20 at 01:54
  • @Barmar only the strings inside { ....} can be saved. – BirdANDBird Nov 10 '20 at 02:31
  • `grep` operates on one line at a time. You can't tell it to match lines between other lines. – Barmar Nov 10 '20 at 02:32
  • Use `sed` or `awk` for this. – Barmar Nov 10 '20 at 02:33
  • @Barmar we can use prep, a similar task see:https://unix.stackexchange.com/questions/232509/how-do-i-find-block-of-text-from-a-text-file-containing-a-specific-string?noredirect=1&lq=1 – BirdANDBird Nov 10 '20 at 02:34
  • 1
    Does this answer your question? [How to print lines between two patterns, inclusive or exclusive (in sed, AWK or Perl)?](https://stackoverflow.com/questions/38972736/how-to-print-lines-between-two-patterns-inclusive-or-exclusive-in-sed-awk-or) – Sundeep Nov 10 '20 at 14:11

1 Answers1

2

You can't do this with grep because it just matches single lines, not groups of lines.

Use sed:

sed -n '/{/,/}/p' test01.txt

This prints the line ranges starting from a line that matches { and ending with a line that matches }. [

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • thanks for your support, it can be solved by using grep see the answer "JdeHaan" from the post: https://unix.stackexchange.com/questions/232509/how-do-i-find-block-of-text-from-a-text-file-containing-a-specific-string?noredirect=1&lq=1 – BirdANDBird Nov 10 '20 at 02:42