print only the first occurrence of a pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting print only the first occurrence of a pattern
# 1  
Old 01-17-2012
print only the first occurrence of a pattern

Hi,
I have a file as below

Code:
select or create proc
/*comments*/
/*comments*/
/*comments*/
/*comments*/
( variables4 datatypes1,
variables1 datatypes2,
variables2 datatypes3,
variables3 datatypes2
)
 
 
some text some text
(
sometext some text
)
some text some text
(
sometext some text
)

Output :-
Code:
variables4 datatypes1,
variables1 datatypes2,
variables2 datatypes3,
variables3 datatypes2

we should search for the first occurrence of ( and ) and print the text inside it.

Last edited by radoulov; 01-17-2012 at 08:01 AM.. Reason: Please use code tags!
# 2  
Old 01-17-2012
Code:
 
$ nawk -F"[()]" 'BEGIN{RS=""}{print $2;exit}' input.txt
variables4 datatypes1,
variables1 datatypes2,
variables2 datatypes3,
variables3 datatypes2

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 01-17-2012
Just a try ..
Code:
$ sed -n '/)/q;p' infile | sed -n '/(/,$p'

# 4  
Old 01-17-2012
thanks its worked :)

Quote:
Originally Posted by itkamaraj
Code:
 
$ nawk -F"[()]" 'BEGIN{RS=""}{print $2;exit}' input.txt
variables4 datatypes1,
variables1 datatypes2,
variables2 datatypes3,
variables3 datatypes2

thanks its worked SmilieSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed print from last occurrence match until the end of last occurrence match

Hi, i have file file.txt with data like: START 03:11:30 a 03:11:40 b END START 03:13:30 eee 03:13:35 fff END jjjjjjjjjjjjjjjjjjjjj START 03:14:30 eee 03:15:30 fff END ggggggggggg iiiiiiiiiiiiiiiiiiiiiiiii I want the below output START (13 Replies)
Discussion started by: Jyotshna
13 Replies

2. Shell Programming and Scripting

awk to extract and print first occurrence of pattern in each line

I am trying to use awk to extract and print the first ocurrence of NM_ and NP_ with a : before in each line. The input file is tab-delimeted, but the output does not need to be. The below does execute but prints all the lines in the file not just the patterns. Thank you :). file tab-delimeted ... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

Print only one occurrence of a file

I have the below file $cat sample.txt one.zip#one.pdf#one.jpeg#1 two.zip#two.pdf#two.jpeg#1 two.zip#two.pdf#two.jpeg,three.jpeg#2 two.zip#two.pdf#two.jpeg,three.jpeg,four.jpeg#3 three.zip#three.pdf#three.jpeg#1 I need the output as one.zip#one.pdf#one.jpeg#1... (8 Replies)
Discussion started by: Bhavi
8 Replies

4. Shell Programming and Scripting

UNIX help to print 50 lines after every 3rd occurrence pattern till end of file

I need help with extract/print lines till stop pattern. This needs to happen after every 3rd occurrence of start pattern and continue till end of file. Consider below is an example of the log file. my start pattern will be every 3rd occurrence of ERROR_FILE_NOT_FOUND and stop pattern will be... (5 Replies)
Discussion started by: NSS
5 Replies

5. Shell Programming and Scripting

Match from one pattern to second occurrence of second pattern

Given an XML file that contains (NOT "consists of"): </dict> <key>system.</key> <dict> <key>rule</key> <string>default</string> </dict> <key>system.burn</key> ... (9 Replies)
Discussion started by: jnojr
9 Replies

6. Shell Programming and Scripting

Changing the first occurrence after matching a pattern

Hi, I got a file which looks like this: Value A Status: - Other: - Value B Status: - Other: - Value C Status: - Other: - I would like to change only the first line which includes the "Status:" string after matching the line containing "Value B", so the output file should look... (5 Replies)
Discussion started by: wenclu
5 Replies

7. Shell Programming and Scripting

Insert new pattern in newline after the nth occurrence of a line pattern - Bash in Ubuntu 12.04

Hi, I am getting crazy after days on looking at it: Bash in Ubuntu 12.04.1 I want to do this: pattern="system /path1/file1 file1" new_pattern=" data /path2/file2 file2" file to edit: data.db - I need to search in the file data.db for the nth occurrence of pattern - pattern must... (14 Replies)
Discussion started by: Phil3759
14 Replies

8. Shell Programming and Scripting

Select everything between first and last occurrence of same pattern

Greetings, I am writing a script which requires as a part, selecting all the lines between the first and last occurrence of a pattern. I have an nawk alternative that is working. But thre should be a generic script that should run on all os viz, linux, sun , aix. The awk script that i... (25 Replies)
Discussion started by: usha rao
25 Replies

9. UNIX for Dummies Questions & Answers

line number of the i-th occurrence of a pattern

Hi all, is there a simple way to obtain the line number of the i-th occurrence of a pattern? I have OCCURRENCE=`grep -io "${STRING_NAME}" ${1}-${8}${EXT}.out_bis| wc -l` which tells me how many occurency I have. I would like to go through them and determine the line number and assign... (6 Replies)
Discussion started by: f_o_555
6 Replies

10. Shell Programming and Scripting

Count the number of occurrences of a pattern between each occurrence of a different pattern

I need to count the number of occurrences of a pattern, say 'key', between each occurrence of a different pattern, say 'lu'. Here's a portion of the text I'm trying to parse: lu S1234L_149_m1_vg.6, part-att 1, vdp-att 1 p-reserver IID 0xdb registrations: key 4156 4353 0000 0000 ... (3 Replies)
Discussion started by: slipstream
3 Replies
Login or Register to Ask a Question