[Solved] Replace first occurrence after match


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [Solved] Replace first occurrence after match
# 1  
Old 01-16-2014
Question [Solved] Replace first occurrence after match

hey guys, i have been trying to work this thing out with sed with no luck Smilie
i m looking for a way to replace only the first occurrence after a match
for example :

Code:
 
Cat
Realized what you gotta do
Dog
Realized what you gotta do
Sheep
Realized what you gotta do
Wolf
Realized what you gotta do

i would like to replace the first occurrence of "you" to "us" only if the word "Sheep" appeared somewhere erailer (could be a random number of lines befor or as a part of a sentence).
so results for this thing will be :

Code:
 
Cat
Realized what you gotta do
Dog
Realized what you gotta do
Sheep
Realized what us gotta do
Wolf
Realized what you gotta do

any help will be appreciated
10x
# 2  
Old 01-16-2014
Try:
Code:
perl -0pe 's/(?<=Sheep)(.*?)you/$1us/s' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 01-16-2014
Try:
Code:
sed '/Sheep/,/you/s/you/us/' file


Last edited by vbe; 01-16-2014 at 10:31 AM.. Reason: typo: missing final ' ...
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 01-16-2014
the perl script worked perfectly, the sed script replaced all the match, and not only the first occurrence.
# 5  
Old 01-16-2014
Hello,

Following may also help.

Code:
awk '/Sheep/ && a!=1 {print;getline; sub(/you/,"us");a=1}1'  file_name

Output will be as follows.

Code:
Realized what you gotta do
Dog
Realized what you gotta do
Sheep
Realized what us gotta do
Wolf
Realized what you gotta do
Sheep
Realized what you gotta do


Thanks,
R. Singh
# 6  
Old 01-17-2014
Hi ,
Thanks all for your reply ..

I have the similar query but for my case I want add another line before that matched line (ie, Realized what you gotta do).

thanks in advance
# 7  
Old 01-17-2014
Some more awk

Code:
$ awk '!f && s{sub(old,new);f=1}/Sheep/{s=1}1' old="you" new="us" file

--edit---

Quote:
Originally Posted by arup1980
Hi ,
Thanks all for your reply ..

I have the similar query but for my case I want add another line before that matched line (ie, Realized what you gotta do).

thanks in advance

Code:
$ awk '!f && s{print newline;f=1} /Sheep/{s=1}1' newline="its my new line" file
Realized what you gotta do
Dog
Realized what you gotta do
Sheep
its my new line
Realized what you gotta do
Wolf
Realized what you gotta do


Last edited by Akshay Hegde; 01-17-2014 at 09:46 AM..
 
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. UNIX for Dummies Questions & Answers

Code for exact match to count occurrence

Hi all, I have an input file as below. I would like to count the occurrence of pattern matching 8th field for each line. Input: field_01 field_02 field_03 field_04 field_05 field_06 field_07 field_08 TA T TA T TA TA TA... (3 Replies)
Discussion started by: huiyee1
3 Replies

3. Shell Programming and Scripting

Egrep - Only Match First Occurrence

echo 'String#1 and String#2' | egrep -o -m 1 'String#.{1}' String#1 String#2 I'm trying to just match the first occurrence of 'String#' + 1 character. I thought the "-m 1" switch would do that for me. Instead I get both occurrences. Can somebody provide some insight? Thanks! (5 Replies)
Discussion started by: sudo
5 Replies

4. Shell Programming and Scripting

[Solved] Last occurrence of a string

I apologize if it was asked before but I couldn't find something related. I want to replace 2 strings in a file e.g pwddb=Lar1wod (need to replace string after =) pwdapp=Wde2xe (need to replace string after =) AND in same file want to find last occurrence of a string (SR2-134561),... (2 Replies)
Discussion started by: J_ang
2 Replies

5. UNIX for Dummies Questions & Answers

[Solved] Awk: count occurrence of each character for every field

Hi, let's say an input looks like: A|C|C|D A|C|I|E A|B|I|C A|T|I|B as the title of the thread explains, I am trying to get something like: 1|A=4 2|C=2|B=1|T=1 3|I=3|C=1 4|D=1|E=1|C=1|B=1 i.e. a count of every character in each field (first column of output) independently, sorted... (4 Replies)
Discussion started by: beca123456
4 Replies

6. Shell Programming and Scripting

sed print from last occurrence match until the end of file

Hi, i have file f1.txt with data like: CHECK a b CHECK c d CHECK e f JOB_START .... I want to match the last occurrence of 'CHECK' until the end of the file. I can use awk: awk '/^CHECK/ { buf = "" } { buf = buf "\n" $0 } END { print buf }' f1.txt | tail +2Is there a cleaner way of... (2 Replies)
Discussion started by: ysrini
2 Replies

7. Shell Programming and Scripting

[Solved] Sed/awk print between patterns the first occurrence

Guys, 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 blablabla bla bla bla bla 2011-08-14 21:10:04,535 blablabla ERROR blablabla bla bla bla ... (6 Replies)
Discussion started by: ppolianidis
6 Replies

8. 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

9. Shell Programming and Scripting

last occurrence of a match through multiple files

Hi all, I have a lot of files with extension ".o" and I would like to extract the 10th line after (last) occurrence of a given string in each of the files. I tried: $ grep "string_to_look_for" *.o -A 10 | tail -1 but it gives the occurrence in the last file with extension .o ... (1 Reply)
Discussion started by: f_o_555
1 Replies

10. Shell Programming and Scripting

Print last occurrence if first field match

Hi All, I have an input below. If the term in the 1st column is equal, print the last row which 1st column is equal.In the below example, it's " 0001 k= 27 " and " 0004 k= 6 " (depicted in bold). Those terms in 1st column which are not repetitive are to be printed as well. Can any body help me... (9 Replies)
Discussion started by: Raynon
9 Replies
Login or Register to Ask a Question