Select text within matching ( ) bracket


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Select text within matching ( ) bracket
# 1  
Old 04-19-2007
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 line.

Here's the input file :

statement statement statement
statement statement statement
statement statement statement

Today( "here"

MONDAY = map( "monday")
TUESDAY = map( "tuesday" )

WESNESDAY = map( "wednesday" )
THURSDAY = map( "thursday" )
FRIDAY = map( "friday" )

SATURDAY = map( "saturday" )
SUNDAY = map( "sunday" )

);

statement statement statement
statement statement statement
statement statement statement

Output file :
--------------

Today( "here"

MONDAY = layer( "monday" )
TUESDAY = layer( "tuesday" )

WESNESDAY = layer( "wednesday" )
THURSDAY = layer( "thursday" )
FRIDAY = layer( "friday" )

SATURDAY = pinText( "saturday" )
SUNDAY = pinText( "sunday" )

);


Thanks in advance,
cursive
# 2  
Old 04-19-2007
Code:
sed -n "/^[^)]*([^)]*$/,/^[^(]*)[^(]*/p" file

# 3  
Old 04-20-2007
Hi,

Abou the code : sed -n "/^[^)]*([^)]*$/,/^[^(]*)[^(]*/p" file
the system prompt error message, "Illegal variable name"

I am not sure whether I missing something here, could someone
show me the right path ?

Thanks in advance,
cursive
# 4  
Old 04-20-2007
Quote:
Originally Posted by cursive
Hi,

Abou the code : sed -n "/^[^)]*([^)]*$/,/^[^(]*)[^(]*/p" file
the system prompt error message, "Illegal variable name"

I am not sure whether I missing something here, could someone
show me the right path ?

Thanks in advance,
cursive
Try using single quotes
Code:
$ cat file
statement statement statement
statement statement statement
statement statement statement

Today( "here"

MONDAY = map( "monday")
TUESDAY = map( "tuesday" )

WESNESDAY = map( "wednesday" )
THURSDAY = map( "thursday" )
FRIDAY = map( "friday" )

SATURDAY = map( "saturday" )
SUNDAY = map( "sunday" )

);

statement statement statement
statement statement statement
statement statement statement
$ sed -n '/^[^)]*([^)]*$/,/^[^(]*)[^(]*/p' file
Today( "here"

MONDAY = map( "monday")
TUESDAY = map( "tuesday" )

WESNESDAY = map( "wednesday" )
THURSDAY = map( "thursday" )
FRIDAY = map( "friday" )

SATURDAY = map( "saturday" )
SUNDAY = map( "sunday" )

);

# 5  
Old 04-20-2007
Hi Anbu23,

A small tweak in the command is finally working well.
I really apperciated for all your help.

Thanks,
cursive
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove bracket including text inside with sed

Hello, I could not remove brackets with text contents myfile: Please remove the bracket with text I wish to remove: I tried: sed 's/\//' myfile It gives: Please remove the bracket with text A1 I expect: Please remove the bracket with text Many thanks Boris (2 Replies)
Discussion started by: baris35
2 Replies

2. 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

3. Shell Programming and Scripting

Difficulties in matching left bracket as literal in awk

I need to work with records having #AX in the EXP1 , please see my data sample and my attempt below: $ cat xx 08:30:33 KEY1 (1255) EXP1 VAL:20AX0030006 08:30:33 KEY1 (1255) EXP1 VAL:20AX0030006 08:30:33 KEY1 (1255) EXP1 VAL:20AW0030006 08:30:33 KEY1 (1255) EXP1 VAL:20AW0030006 $ gawk '{... (1 Reply)
Discussion started by: migurus
1 Replies

4. 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

5. 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

6. Shell Programming and Scripting

ksh, difference between double bracket and single bracket

Can somebody tell me the difference between double brackets and single brackets, when doing a test. I have always been acustomed to using single brackets and have not encountered any issues to date. Why would somebody use double brackets. Ie if ] vs if Thanks to... (2 Replies)
Discussion started by: BeefStu
2 Replies

7. 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

8. 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

9. Shell Programming and Scripting

Group on the basis of common text in the square bracket and sorting

File A 99 >ac >ss >juk 70 >acb >defa 90 >ca 100 >aa >abc >bca 85 >cde 81 >ghi >ghij 87 >def >fgh <ijk 89 >fck >ghij >kill >aa The given output shud be 100 >aa >abc >bca 87 >def >fgh <ijk 89 >fck >ghij >kill >aa (2 Replies)
Discussion started by: cdfd123
2 Replies

10. Shell Programming and Scripting

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., ABC=100{ DEF = 200 { GHI, JKL } } #2nd Block 123 { 456{78,910}} }I am trying to figure out how to remove... (2 Replies)
Discussion started by: BootComp
2 Replies
Login or Register to Ask a Question