Substitution in first occurrence with sed


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Substitution in first occurrence with sed
# 1  
Substitution in first occurrence with sed

I have the following script:

Code:
sed '/string1/,/string2/!d' infile

I want to apply the script to the first occurrence only. I have tried

Code:
sed '0,/string1/,/string2/!d' infile

Of course, that does not work
Any help will be greatly appreciated

Last edited by Xterra; 03-18-2016 at 11:18 PM..
# 2  
Hello Xterra,

Following may help you in same, let's say we have following Input_file.
Code:
cat Input_file
string1 string3 string4 string1

Then following is the code which may help you in same.
Code:
sed 's/string1/string2/1'  Input_file

Output will be as follows.
Code:
string2 string3 string4 string1

Hope this helps you.

Thanks,
R. Singh
# 3  
RavinderSingh13

Not exactly. This is my file:
Code:
string1
I want this text
string2
string3
something else here
string4
string1
But I definitively DO NOT want this text
string2
End

If I use my code, this is what I get:
Code:
string1
I want this text
string2
string1
But I definitively DO NOT want this text
string2

This is what I need:
Code:
string1
I want this text

I modified my code hoping it would work:
Code:
sed '0,/string1/,/string2/!d'

But of course it didn't:
Code:
sed: -e expression #1, char 12: unknown command: `,'

Thus, your code will not produce the desired output
# 4  
With your sample input, the search for string1 seems to be a red herring. Try:
Code:
sed '/string2/q'


Last edited by Don Cragun; 03-19-2016 at 05:23 PM.. Reason: Simplified command (use q instead of d).
# 5  
Try:
Code:
sed -n '/string2/q; /string1/,/string2/p' file




--
To print including string2:
Code:
sed -n '/string1/,/string2/p; /string2/q' file


Last edited by Scrutinizer; 03-19-2016 at 09:48 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Quote:
Originally Posted by Don Cragun
With your sample input, the search for string1 seems to be a red herring.
With the sample input: yes. But if the "string1" happens to be not on the starting line of the input file there would be a difference. Your code would print everything from first line to a line with "string2".

This is why I'd prefer Scrutinizers solution even if your code is shorter.

bakunin
# 7  
Quote:
Originally Posted by Xterra
Not exactly. This is my file:
Code:
string1
I want this text
string2
string3
something else here
string4
string1
But I definitively DO NOT want this text
string2
End

This is what I need:
Code:
string1
I want this text

Code:
perl -ne 'if(/string1/../string2/){exit if /string2/; print}' xterra.input

Code:
string1
I want this text

Code:
perl -ne 'if(/string1/../string2/){print; exit if /string2/}' xterra.input

Code:
string1
I want this text
string2

This User Gave Thanks to Aia For This Post:
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #649
Difficulty: Easy
aterm is an rxvt based terminal emulator developed for Afterstep.
True or False?

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. UNIX for Dummies Questions & Answers

Sed, last occurrence

How to find last occurrence of a keyword in a file using sed. (4 Replies)
Discussion started by: nexional
4 Replies

3. Shell Programming and Scripting

Modifying sed to only change last occurrence.

I'm using sed to switch integers (one or more digits) to the other side of the ':' colon. For example: "47593:23421" would then be "23421:47593". The way it functions right now, it is messing my settings file to use with gnuplot. The current command is: sed 's/\(*\):\(*\)/\2:\1/' out3 >... (3 Replies)
Discussion started by: D2K
3 Replies

4. Shell Programming and Scripting

Sed diffrent replace by occurrence

I couldn't find the answer anywhere, so I hope you could help me. I need to change something like the following: something/bla/aaaa anything/bbb to: something --bla ----aaaa anything --bbb How do I do this? Is it possible with sed? I tried various patterns, but don't know how to... (5 Replies)
Discussion started by: Patwan
5 Replies

5. Shell Programming and Scripting

Sed/awk print between different patterns the first occurrence

Thanks for the help yesterday. I have a little modification today, I am trying the following: i have a log file of a webbap which logs in the following pattern: 2011-08-14 21:10:04,535 blablabla ERROR Exception1 blablabla bla bla bla bla 2011-08-14... (2 Replies)
Discussion started by: ppolianidis
2 Replies

6. Shell Programming and Scripting

Replace x Number of String Occurrence with Sed

Ok, So I have a huge file that has over 12000 lines in it. in this file, there are 589 occurrences of the string "use five-minute-interval" spread in various areas in the file. How can i replace the the last 250 of the occurrences of "use five-minute-interval" with "use... (10 Replies)
Discussion started by: SkySmart
10 Replies

7. Shell Programming and Scripting

SED to replace exact match, not first occurrence.

Lets say I have file.txt: (Product:Price:QuantityAvailable) (: as delimiter) Chocolate:5:5 Banana:33:3 I am doing a edit/update function. I want to change the Quantity Available, so I tried using the SED command to replace 5, but my Price which is also 5 is changed instead. (for the Banana... (13 Replies)
Discussion started by: andylbh
13 Replies

8. Shell Programming and Scripting

Using sed to substitute first occurrence

I am trying to get rid of some ending tags but I run into some problems. Ex. How are you?</EndTag><Begin>It is fine.</Begin><New> Just about I am trying to get rid of the ending tags, starts with </ and ending with >. (which is </EndTag> and </Begin>) I tried the following sed... (2 Replies)
Discussion started by: quixoticking11
2 Replies

9. Shell Programming and Scripting

SED replace string by occurrence

hi all, I have a text file with following content PAGENUMBER asasasa asasasa PAGENUMBER sasasasasa PAGENUMBER using sed i want to replace PAGENUMBER by occurrence count eg 1 asasasa asasasa 2 sasasasasa 3 (4 Replies)
Discussion started by: uttamhoode
4 Replies

10. Shell Programming and Scripting

Grep for the same occurrence or maybe Sed

Hi, I have a file that looks like this dasdjasdjoasjdoasjdoa SYN dakspodkapsdka asdasdasdasdasdasdasd SYN sdfsdfsdfsdfdf shfishifhsdifhsidhfif fsdfsdfsdfsdfs sdfsdfsdfsdsdfsdfsdff cercercercerce sdasdajsdoajsodasodoo FIN dasdaskdpasdda... (4 Replies)
Discussion started by: hcclnoodles
4 Replies

Featured Tech Videos