sed: how to limit pattern search to first instance only


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed: how to limit pattern search to first instance only
# 1  
Old 01-16-2011
sed: how to limit pattern search to first instance only

I need to reduce a file's size below 50MB by deleting chucks of text. The following sed does this.
Code:
sed '/^begpattern/,/endpattern/d' myfile

However, it's possible that the file size can get below 50MB by just deleting the first instance of the pattern. How do I code that into sed?
Or can awk do this?

Last edited by Franklin52; 01-17-2011 at 04:27 AM.. Reason: Please use code tags
# 2  
Old 01-17-2011
Yes awk can do this:
Code:
awk '!s&&/^begpattern/ {s=++d} !d ; /endpattern/ {d=0}' infile

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 01-17-2011
I've never seen this construct before. Can you explain what's going on? Smilie
# 4  
Old 01-17-2011
OK here is the explination:

s variable is the started flag, and the statement !s&&/^begpattern/ will only match a begpattern if s if false (unassigned).

s=++d increments d (our delete flag) from unassigned to 1, and also assigns s to this incremented value ie 1.

!d only print a line if d is false/zero.

/endpattern/ {d=0} reset d (delete flag) back to false/zero when endpattern is seen.

I hope someone can comes up with a sed solution for you, I spent ages mucking around with sed trying to get it done, but my sed skills aren't up to it Smilie
# 5  
Old 01-17-2011
Code:
 xSED=`sed -ne '/begpattern/,/endpattern/{=;!p;{/endpattern/q;}}' infile|sed -n '1p;$p'|sed 'N;s/\n/,/'` ; sed "$xSED d" infile

regards
ygemici
# 6  
Old 01-17-2011
Yikes, need to run sed 3 times! This is the closest I got but 1st endpattern is still printed:

Code:
sed -n -e '1,/^begpattern/{/^begpattern/!p;};' -e '/endpattern/,$p'

# 7  
Old 02-23-2011
My sed skills are now up to solving this, I knew it should be possible to do it in 1 pass:

Code:
sed -n -e ':b' -e '/^begpattern/bs;p;n;bb' -e ':s' -e '/^endpattern/br;n;bs' -e ':r' -e 'n;p;br'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed - Search and replace within pattern

Hi Guys! Unix newbie here! Have a requirement for which I have been scouting the forums for a solution but has been out of luck so far :( I have a file which contains the following:- TEST1|TEST2|"TEST3|1@!2"|TEST5 My sed command should result in either one the following output:-... (6 Replies)
Discussion started by: hishamzz
6 Replies

2. Shell Programming and Scripting

How to use sed to search a particular pattern in a file backward after a pattern is matched.?

Hi, I have two files file1.txt and file2.txt. Please see the attachments. In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to... (9 Replies)
Discussion started by: saurabh kumar
9 Replies

3. Shell Programming and Scripting

sed for first instance of a pattern

Hi Everyone, I have the below information from a log file: LOAD SUMMARY ============ WRT_8036 Target: TGT_1_TAB (Instance Name: ) WRT_8039 Inserted rows - Requested: 3929 Applied: 0 Rejected: 3929 Affected: 0 Mutated from update: 3929 WRT_8041 Updated rows ... (7 Replies)
Discussion started by: galaxy_rocky
7 Replies

4. Shell Programming and Scripting

sed command to print first instance of pattern in range

The following text is in testFile.txt: one 5 two 10 three 15 four 20 five 25 six 10 seven 35 eight 10 nine 45 ten 50 I'd like to use sed to print the first occurance of search pattern /10/ in a given range. This command is to be run against large log files, so to optimize efficiency,... (9 Replies)
Discussion started by: uschaafm
9 Replies

5. Shell Programming and Scripting

sed search pattern and delete lines

Hello, i have a question. My problem is that i have a file like: TEST JOHN ADAM MICHAEL SEBASTIAN ANDY i want find for MICHAEL and want delete lines like this: TEST (4 Replies)
Discussion started by: eightball
4 Replies

6. Shell Programming and Scripting

Search pattern within file using sed..

Hi, Could someone help me in figuring out a way using which i can search for a specific pattern. Eg. JUSTDOIT..I have to print just the word "DO" from "JUSTDOIT" If the same word JUSTDOIT is print n number of times (6 Replies)
Discussion started by: sankasu
6 Replies

7. Homework & Coursework Questions

pattern search with sed

Hi, I want to do replacing of some pattern by using sed. pattern : " white space / to white space / script is : sed -e s/\"\$hi/\$hi/ \ -e s/\"\ \\/\\/ \ test.res > test.output + sed -e 's/"$hi/$hi/' -e 's/" \/\/' test.res sed: -e expression #2, char 8: unterminated `s'... (5 Replies)
Discussion started by: bhrat kapoor
5 Replies

8. Shell Programming and Scripting

unable to use new line in sed search pattern.

hi , my lilo.conf is as shown below : prompt default=Primary read-only image=/boot/bzImage label=Primary root=/dev/md0 append="reboot=t md=0 ip=off panic=5 quiet console=ttyS0,115200n81" read-only image=/boot/bzImage label=Recover root=/dev/sda3 ... (7 Replies)
Discussion started by: success
7 Replies

9. Shell Programming and Scripting

Multiline pattern search using sed or awk

Hi friends, Could you please help me to resolve the below issue. Input file :- <Node> <username>abc</username> <password>ABC</password> <Node> <Node> <username>xyz</username> <password>XYZ</password> <Node> <Node> <username>mnp</username> ... (3 Replies)
Discussion started by: haiksuresh
3 Replies

10. Shell Programming and Scripting

SED Search Pattern and Replace with the Pattern

Hello All, I have a string "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031", and I just want to extract LLSV1, but I dont get the expected result when using the sed command below. # echo "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031" | awk '{print... (4 Replies)
Discussion started by: racbern
4 Replies
Login or Register to Ask a Question