Search only the first occurence in shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search only the first occurence in shell
# 1  
Old 04-19-2013
Search only the first occurence in shell

Hello
I have a configuration file and I want to extract a part of this configuration.

Example of configuration:

Code:
profile toto {
 bla bla
 blabla
}

conftest {
  toto {
  myarguements
  }

I try to do a sed command:
Code:
sed -n '/'toto' {$/,/^}/p'

But the result is :
Code:
profile toto {
 bla bla
 blabla
}
  toto {
  myarguements
  }

Is it possible to have only the first occurence of the result?
Or find a solution which say to have the result of toto in the first line and } as first character of the line (whithout space or tabulation)?

The result I want is the following:

Code:
profile toto {
 bla bla
 blabla
}

Regards.
# 2  
Old 04-19-2013
Maybe this is what you are looking for?
Code:
$ sed -n "/profile toto/,/}/ p" config
profile toto {
 bla bla
 blabla
}

# 3  
Old 04-19-2013
Hello,
No,
the occurrence toto is a variable called in an other context.
The result of this variable is toto, not profile toto.
I must find a solution only with toto
(it would have been too easy ^_^)
# 4  
Old 04-19-2013
Then it's kind of unclear what you are trying to do. Is toto a variable name or the literal text? Is the following your intent? If not, then a better example would help. Smilie
Code:
$ var=toto
$ sed -n "/profile $var/,/}/ p" config
profile toto {
 bla bla
 blabla
}

# 5  
Old 04-19-2013
I also have find on this solution.

Code:
$ var=toto
$ sed -n "/profile $var/,/}/ p" config
profile toto {
 bla bla
 blabla
}

The problem is between profile et $var I can have a different values:
example: profile http $var, profile https $var, profile tcp $var and other things.

Is it possible to say that searched paramater contains profile and $var to }

Last edited by maverick31; 04-19-2013 at 05:34 AM..
# 6  
Old 04-19-2013
Quote:
profile http $var, profile https $var, profile tcp $var and other things.
All right. Then do this:
Code:
$ var=toto
$ sed -n "/profile.* $var/,/}/ p" config
profile toto {
 bla bla
 blabla
}

# 7  
Old 04-19-2013
Hello,
This solution is functionnal.

I would like to find also a solution a little more generic.

I find this command which also answer on my problem:

sed '1,/toto/d;/}/,$d'

The problem is is return me

Code:
 bla bla
 blabla

The first line and last line has been deleted.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

To find the Nth Occurence of Search String

Hi guys, I like to find the Line number of Nth Occurence of a Search string in a file. If possible, if it will land the cursor to that particualar line will be great. Cheers!! (3 Replies)
Discussion started by: mac4rfree
3 Replies

2. UNIX for Dummies Questions & Answers

Search specific pattern in file and return number of occurence

Hi I want to search for a specific pattern in file Say ABC;HELLO_UNIX_WORLD;PQR ABC;HELLO_UNIX_WORLD_IS_NOT_ENOUGH;XYZ ABC;HELLO_UNIX_FORUM;LMN Pattern to search is : "HELLO_UNIX_*****" and not "HELLO_UNIX_***_***_" I mean after "HELLO_UNIX" there can only be one word.In this case... (2 Replies)
Discussion started by: dashing201
2 Replies

3. Shell Programming and Scripting

shell script to find the second occurence of the alphabet in a string

this is my assignment question. i'm supposed to submit it tommorow. can somebody please help me with it? Do not post homework questions in the main forums. Please post in the homework forum using the correct template. (0 Replies)
Discussion started by: vijjy
0 Replies

4. Shell Programming and Scripting

Search file for pattern2 starting from occurence of pattern1

Hi folks, I have a file which contains several occurences of 2 different patterns. I need to find out the line of first occurence of pattern2 starting after the position of first occurence of pattern1. example file: aaaa pattern2 bbbb pattern1 ccc pattern2 ddd pattern1 eee pattern2... (9 Replies)
Discussion started by: sameucho
9 Replies

5. UNIX for Dummies Questions & Answers

How to search unique occurence in a file?

Hi, I have to search and count unique occurence of DE numbers in bold below in a file which has content like below. Proc Tran F-BUY Item Tkey Q5JV Item Tsid JTIZ9 Item Tdat 20091001 Item Tset 20091001 Item Tbkr 5 Item Tshs 2 Item Tprc 897.0 Item Tcom 2000.0 Item Tcm1 20091001... (6 Replies)
Discussion started by: akash028
6 Replies

6. Shell Programming and Scripting

Perl : Search for next occurence of a word in an array

I have an array as follows: Space: ABC Name: def Age: 22 Type: new Name: fgh Age: 34 Type: old Space: XYZ Name: pqr Age: 44 Type: new : : How can I separate the array with elements starting from Space:ABC until Space: XYZ & put them in a different array & so on... (4 Replies)
Discussion started by: deo_kaustubh
4 Replies

7. Shell Programming and Scripting

search file between last occurence of 2 strings

I need to extract the last block of /== START OF SQLPLUS ==/ and /== END OF SQLPLUS ==/. The logifle is written to several times in a day using >> to append. I need a solution using grep/sed. logfile looks like this START OF LOGFILE /== START OF SQLPLUS ==/ ERROR /== END OF SQLPLUS... (5 Replies)
Discussion started by: hanton
5 Replies

8. Shell Programming and Scripting

Perl onliner to search the last line with an occurence of a pattern

Hi I need a perl onliner which seaches a line starting with a pattern(last occurence) and display it. similar to grep 'pattern' filename | tail -1 in UNIX Ex: I want to display the line starting with "cool" and which is a last occurence adadfadafadf adfadadf cool dfadfadfadfara... (4 Replies)
Discussion started by: ammu
4 Replies

9. UNIX for Dummies Questions & Answers

search& count for the occurence of a word

Greetings, I need to search and count all the occurences of a word in all the files in a directory. Any suggestions greatly appreciated. Thanks (1 Reply)
Discussion started by: skoppana
1 Replies

10. Shell Programming and Scripting

Finding Occurence of comma in a Variable ( KORN Shell)

All I want to find the occurence of comma in a variable in KORN shell. For example : var = test,test2,test3 .... finding occurence of comma in this variable. Result = 3 now. Please help me to write the code. Thanks in advance. Regards Deepak (2 Replies)
Discussion started by: DeepakXavier
2 Replies
Login or Register to Ask a Question