Sed grep the first matched lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed grep the first matched lines
# 1  
Old 07-13-2009
Sed grep the first matched lines

Hi

I have a myfile.txt contains the following:

Code:
CONTEXT {
  AAAAA
}
...
CONTEXT {
  BBBBB
}

I want to extract the lines in between CONTEXT { ... }, one by one.
Hence I wrote a command like the following,

Code:
sed -n '/^CONTENT/,/^}/ {
w a.txt
}' myfile.txt

The problem with this command is it will extract all the CONTEXT { ... } and write to a.txt.

How can I write only the first matched lines, e.g. CONTEXT {
AAAAA
} ???
# 2  
Old 07-13-2009
yep - sed is greedy :-)

try:

Code:
#  sed -n '/^CONTEXT/,/^}/ {p;/^}/q;}' infile
CONTEXT {
  AAAAA
}

Or in your case, to write to file:

#  sed -n '/^CONTEXT/,/^\}/ {
w a.txt
/^}/q; }' infile

HTH

Last edited by Tytalus; 07-13-2009 at 07:12 AM.. Reason: orig put in just to print - added write to file as per posters question
# 3  
Old 07-13-2009
Hi Tytalus

Your command works perfectly, but I'm trying to understand how your pattern "/^}/q;" works, could you explain or point me to the some other resource?
# 4  
Old 07-13-2009
Code:
/^}/  # match any  line beginning with }

q       #  quit

i.e. , as this gets parsed in the main section, it will print each line, until it hits this} at the start, and then it will bomb itself out.

I tend to use this style quite a lot...e.g. sed 9q infile will act just like head -9 infile, but saves you a character when typing ;-)

HTH
# 5  
Old 07-13-2009
Trying to follow your idea, but this time is to delete the first matched block with the following command:
Code:
sed '/^CONTEXT/,/^}/ {d;/^}/q;}' c.txt

Unfortunately, this command does not quite when the next } matched and it deleted all the blocks ...


What did I miss here?
# 6  
Old 07-13-2009
ah - here it did delete the fist block.

output :

Code:
#  sed '/^CONTEXT/,/^}/ {d;/^}/q;}' infile
...

But...the problem is that as it quits when it gets to the ^} line, the rest of the file doesn't get parsed at all....
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed: how to merge two lines moving matched pattern to end of previous line

hello everyone, im new here, and also programming with awk, sed and grep commands on linux. In my text i have many lines with this config: 1 1 4 3 1 1 2 5 2 2 1 1 1 3 1 2 1 3 1 1 1 2 2 2 5 2 4 1 3 2 1 1 4 1 2 1 1 1 3 2 1 1 5 4 1 3 1 1... (3 Replies)
Discussion started by: satir
3 Replies

2. Shell Programming and Scripting

Grep command to return all the lines between one matched pattern to another.

14:15:00- abcdefghijkl. 14:30:00- abcdefghijkl. 14:35:00- abcdefghijkl. 123456789. 123456789. 14:45:00- abcdefghijkl. 14:50:00- abcdefghijkl. 123456789. 15:30:00-abcdefghijkl. (3 Replies)
Discussion started by: dev_shivv
3 Replies

3. Solaris

Grep command to return all the lines from one matched pattern to another.

For example a log file looks like below- 13:30:00- abcdefghijklhjghjghjhskj. abcdefghijkl. 14:15:00- abcdefghijkl. 14:30:00- abcdefghijkl. 14:35:00- abcdefghijkl. 123456789. 123456789. 14:45:00- abcdefghijkl. (0 Replies)
Discussion started by: dev_shivv
0 Replies

4. Shell Programming and Scripting

grep/sed to remove lines in file

Hi, I have a file with values, file1: BELL-1180-1180-81|577:1017| BELL-1180-1180-81|jm10i-auto-stub1/577:102| BELL-1180-1180-81|jm10i-auto-stub1/577:101| BELL-1180-1180-81|jm10i-auto-stub1/577:1700| BELL-1180-1180-81|jm10i-auto-stub1/577:1699| I need to remove the lines which has... (9 Replies)
Discussion started by: giri_luck
9 Replies

5. Shell Programming and Scripting

How to get lines started with matched strings using sed or grep for loop?

I have a huge file and want to separate it into several subsets. The file looks like: C1 C2 C3 C4 ... (variable names) 1 .... 2 .... 3 .... : 22 .... 23 .... I want to separate the huge file using the column 1, which has numbers from 1 to 23 (but there are different amount of... (8 Replies)
Discussion started by: AMBER
8 Replies

6. Shell Programming and Scripting

Like grep -v for a string over 2 lines? Sed?

Hi, I have a log file that I need to monitor as it's being written to, and I want to exclude certain strings from the output. At the moment I'm using ... tail -f LogFileName_`date +%d`.log | egrep -v "First String To Exclude | 2nd string | 3rd string" ...which works OK - but now I need to... (1 Reply)
Discussion started by: jake657
1 Replies

7. UNIX for Dummies Questions & Answers

retrieve lines using sed, grep or awk

Hi, I'm looking for a command to retrieve a block of lines using sed or grep, probably awk if that can do the job. In below example, By searching for words "Third line2" i'm expecting to retrieve the full block starting with 'BEGIN' and ending with 'END' of the search. Example: ... (3 Replies)
Discussion started by: learning_linux
3 Replies

8. Shell Programming and Scripting

print only matched string instead lines in grep

frnd, Is there any way to print only the string that matched the pattern instead printing the whole line? thanks in advance. (3 Replies)
Discussion started by: clx
3 Replies

9. Shell Programming and Scripting

SED: match pattern & delete matched lines

Hi all, I have the following data in a file x.csv: > ,this is some text here > ,,,,,,,,,,,,,,,,2006/11/16,0.23 > ,,,,,,,,,,,,,,,,2006/12/16,0.88 < ,,,,,,,,,,,,,,,,this shouldnt be deleted I need to use SED to match anything with a > in the line and delete that line, can someone help... (7 Replies)
Discussion started by: not4google
7 Replies

10. Shell Programming and Scripting

sed, grep, awk, regex -- extracting a matched substring from a file/string

Ok, I'm stumped and can't seem to find relevant info. (I'm not even sure, I might have asked something similar before.): I'm trying to use shell scripting/UNIX commands to extract URLs from a fairly large web page, with a view to ultimately wrapping this in PHP with exec() and including the... (2 Replies)
Discussion started by: ropers
2 Replies
Login or Register to Ask a Question