-1

I have a file:

   [{ 
    abc
    def
    123
   }]
    
[{
    abc
    456
    789
}]

var="789" i want find (grep sed ...) $var return results Curly braces :

        {
                abc
                456
                789
        }
    
or
       [{
            abc
            456
            789
        }]
    
or
            abc
            456
            789

i try: grep '[.*${var}.*]' file but return results:

{ 
        abc
        def
        123
       }
        
    {
        abc
        456
        789
    }
2
  • 2
    What have you tried?
    – mtk
    Jul 12 '21 at 7:37
  • 1
    The post is updated more
    – toilaai
    Jul 12 '21 at 7:48
0

If the blocks are separated by empty lines, then you can read the file in "paragraph" mode:

awk -v RS="" '/789/' file

If, like here, the blocks are separated by "blank" lines (there are tabs or spaces in the separating line), then the record separator is a bit more complicated:

awk -v RS='\n([[:blank:]]+\n)+' '/789/' file

To pass the pattern as a variable:

awk -v RS="" -v patt="$var" '$0 ~ patt' file
0

To pass the pattern as a variable:

awk -v RS="" -v patt="$var" '$0 ~ patt' file

it return results as value orginal:

 [{ 
    abc
    def
    123
   }]
    
[{
    abc
    456
    789
}]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.