Skip to main content
added 106 characters in body
Source Link
Stéphane Chazelas
  • 586.3k
  • 96
  • 1.1k
  • 1.7k

I have a file like this:-

pin(ABC) {
a b c d e f {
abc
}
}
pin(XYZ) {
g h i j k {
j k {
cg {
}
}
}
}
abcd pqrs rstu
mango banana tree
pin(PQR) {
mango
}

pin(ABC) { a b c d e f { abc } } pin(XYZ) { g h i j k { j k { cg { } } } } abcd pqrs rstu mango banana tree pin(PQR) { mango } 

Now i want to grep the text like this:-
when i give ABC as input then output should be a file named ABC with the content from "pin(ABC) {" upto matching bracket "}" like this :-

pin(ABC) {
a b c d e f {
abc
}
}

pin(ABC) { a b c d e f { abc } } 

when i give PQR the output should be a file named PQR with this content:-

pin(PQR) {
mango
}

pin(PQR) { mango } 

& so on for any word which is inside pin()
one way to do it is to grep the text from "pin(ABC" till next name "pin" & direct the output to the file named "ABC" , it works for only ABC but fails for case of PQR & XYZ. How to do it.

I have a file like this:-

pin(ABC) {
a b c d e f {
abc
}
}
pin(XYZ) {
g h i j k {
j k {
cg {
}
}
}
}
abcd pqrs rstu
mango banana tree
pin(PQR) {
mango
}

Now i want to grep the text like this:-
when i give ABC as input then output should be a file named ABC with the content from "pin(ABC) {" upto matching bracket "}" like this :-

pin(ABC) {
a b c d e f {
abc
}
}

when i give PQR the output should be a file named PQR with this content:-

pin(PQR) {
mango
}

& so on for any word which is inside pin()
one way to do it is to grep the text from "pin(ABC" till next name "pin" & direct the output to the file named "ABC" , it works for only ABC but fails for case of PQR & XYZ. How to do it.

I have a file like this:-

pin(ABC) { a b c d e f { abc } } pin(XYZ) { g h i j k { j k { cg { } } } } abcd pqrs rstu mango banana tree pin(PQR) { mango } 

Now i want to grep the text like this:-
when i give ABC as input then output should be a file named ABC with the content from "pin(ABC) {" upto matching bracket "}" like this :-

pin(ABC) { a b c d e f { abc } } 

when i give PQR the output should be a file named PQR with this content:-

pin(PQR) { mango } 

& so on for any word which is inside pin()
one way to do it is to grep the text from "pin(ABC" till next name "pin" & direct the output to the file named "ABC" , it works for only ABC but fails for case of PQR & XYZ. How to do it.

Source Link

grep upto matching brackets

I have a file like this:-

pin(ABC) {
a b c d e f {
abc
}
}
pin(XYZ) {
g h i j k {
j k {
cg {
}
}
}
}
abcd pqrs rstu
mango banana tree
pin(PQR) {
mango
}

Now i want to grep the text like this:-
when i give ABC as input then output should be a file named ABC with the content from "pin(ABC) {" upto matching bracket "}" like this :-

pin(ABC) {
a b c d e f {
abc
}
}

when i give PQR the output should be a file named PQR with this content:-

pin(PQR) {
mango
}

& so on for any word which is inside pin()
one way to do it is to grep the text from "pin(ABC" till next name "pin" & direct the output to the file named "ABC" , it works for only ABC but fails for case of PQR & XYZ. How to do it.