Select block of text around matching braces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Select block of text around matching braces
# 1  
Old 03-10-2009
Select block of text around matching braces

Hi,
I have several block of text that I need to select, however this text may be spread over several lines and contains the '{' and '}' within it.
For e.g.,
Code:
ABC=100{
  DEF = 200
  {
     GHI,
     JKL
  }
}
#2nd Block
123
{
   456{78,910}}
}

I am trying to figure out how to remove the newline characters from the blocks above so that my further processing would be easier.
i.e. I need to get the output something like this,
Code:
ABC=100{DEF=200{GHI,JKL}}
123{456{78,910}}}

Am quite new to shell scripting so any help with this is greatly appreciated.

TIA
# 2  
Old 03-10-2009
Code:
#!/usr/bin/perl
undef $/;
open FH,"<a.txt";
$str=<FH>;
@arr=split(/#[0-9]+.* Block/,$str);
map {s/[\n ]//g} @arr;
print join "\n",@arr;

# 3  
Old 03-10-2009
Quote:
Originally Posted by summer_cherry
Code:
#!/usr/bin/perl
undef $/;
open FH,"<a.txt";
$str=<FH>;
@arr=split(/#[0-9]+.* Block/,$str);
map {s/[\n ]//g} @arr;
print join "\n",@arr;

Thanks. But would be great if I could explanation of the above code for my understanding.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How do I select certain columns with matching pattern and rest of the lines?

I want to select 2nd, 3rd columns if line has "key3" and print rest of the lines as is. # This is my sample input key1="val1" key2="val2" key3="val3" key4="val4" some text some text some text some text key1="val1" key2="val2" key3="val3" key4="val4" some text some text some text some... (3 Replies)
Discussion started by: kchinnam
3 Replies

2. Shell Programming and Scripting

Grep text matching problem with script which checks if web page contains text.

I wrote a Bash script which checks to see if a text string exists on a web page and then sends me an email if it does (or does not e.g. "Out of stock"). I run it from my crontab, it's quite handy from time to time and I've been using it for a few years now. The script uses wget to download an... (6 Replies)
Discussion started by: gencon
6 Replies

3. Shell Programming and Scripting

Like to select text in a From/To list

Hi I need help to configure AWK to find a string based From/To filed I have a table like this 0A00 - 0AFF Nuts 0B00 - 0BFF Bolt If I have in a program a value like "0B22" I wold like to get "Bolt" in return. List are in Hex Still try to learn AWK :) (3 Replies)
Discussion started by: Jotne
3 Replies

4. Shell Programming and Scripting

Select the exact matching contents using grep

Hi everyone I've two files.. The contents of file1 are as shown below 4 5 12 13 36 37 45 46 47 The contents of file2 are as shown below 21 hello 13 world (5 Replies)
Discussion started by: abk07
5 Replies

5. Shell Programming and Scripting

Block of records to select from a file

Hello: I am new to shell script programming. Now I would like to select specific records block from a file. For example, current file "xyz.txt" is containing 1million records and want to select the block of records from line number 50000 to 100000 and save into a file. Can anyone suggest me how... (3 Replies)
Discussion started by: nvkuriseti
3 Replies

6. UNIX for Dummies Questions & Answers

How to select text within the second ()?

Hello, Can someone advise me how to select the text within the second bracket of a string in shell script? For example, the input file: some message (string A) some message (string B) some message (string C) some message (string D) some message (string E) The number of bracket is random... (4 Replies)
Discussion started by: hanul
4 Replies

7. Shell Programming and Scripting

how to find matching braces using sed or in shell script

hi, I want to print all the lines between the matching braces. For example,the file contains like the below. asdfsdf fsdfsd WO{ w1{ ada ... (3 Replies)
Discussion started by: Boopesh
3 Replies

8. Shell Programming and Scripting

Finding the line number of matching braces

Hi,I am new to shell scripting and i want to find the line numbers of matching braces. The file contents are as follows File XXX.dat 1 ( CLASS "FRUIT" 2 (TYPE "PERSISTENT") 3 (MESSAGE_TYPE "M") 4 (GET_REQRD "Y") 5 (SET_REQRD "Y") 6 ) 7 ( CLASS... (3 Replies)
Discussion started by: Rajendra_1510
3 Replies

9. UNIX for Dummies Questions & Answers

Select text within matching ( ) bracket

Hi, I am looking for a simple command to select text within a open bracket "(" and a matching close bracket ")" and output the within-bracket-text to a file. This function is similar to the common vi select a range of text with "(" to ")" but not sure how to run the same function in command... (4 Replies)
Discussion started by: cursive
4 Replies

10. Shell Programming and Scripting

Select last block from a file

Hi, I have to always select last portion of a block identified by 2 fixed keywords which repeats in a file for eg START .... ... END START .... ... END START .... ... END (9 Replies)
Discussion started by: misenkiser
9 Replies
Login or Register to Ask a Question