Search and replace only the first occurance


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search and replace only the first occurance
# 1  
Old 10-13-2011
Question Search and replace only the first occurance

Hi,

I need to search a string and replace with nothing, but only the First occurring string

using sed/perl/awk
# 2  
Old 10-13-2011
its simple .. wats your try ..
# 3  
Old 10-13-2011
Quote:
Originally Posted by jayan_jay
its simple .. wats your try ..
tried this, but didn't work
Code:
sed 's/\(.*\)'$line'/\1/' $file_cases > $file_temp_cases_tmp

also tried
Code:
sed 's/string/replace/' filename

but it replaces all the string


Moderator's Comments:
Mod Comment Video tutorial on how to use code tags in The UNIX and Linux Forums.

Last edited by Franklin52; 10-13-2011 at 05:14 AM.. Reason: Please use code tags, thank you
# 4  
Old 10-13-2011
with slight modification ..
Code:
$ sed 's/string/replace/1' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help search and replace the last occurance of match in a file

Hi I want to replace only the last occurance of "union all" in input file with ";" I tried with sed 's/union all/;/g' in my input file, it replaced in all lines of input file Eg: select column1,column2 from test1 group by 2 union all select column1,column2 from test2 group by 2 union all ... (9 Replies)
Discussion started by: antosr7
9 Replies

2. Shell Programming and Scripting

Replace 3rd occurance of SPACE with newline

I have file with SQL output as 0001 firstname1 lastname1 0002 firstname2 lastname2 0003 firstname3 lastname3 0004 firstname4 lastname4 Expected output : 0001 firstname1 lastname1 0002 firstname2 lastname2 0003 firstname3 lastname3 0004 firstname4 lastname4 Let me know if this can... (9 Replies)
Discussion started by: sameermohite
9 Replies

3. Shell Programming and Scripting

replace every second occurance of a string

I want to replace every 2nd occurance of a string/character from a line. ababacbsbddbsbbcbdbssb i want to replace every s2nd b with @ like below should be like aba@acbs@ddbs@bc@dbss@ (3 Replies)
Discussion started by: ratheeshjulk
3 Replies

4. Shell Programming and Scripting

replace/delete odd occurance

how to delete/replace the the odd occurance of a pattern say newline character "\n" a,b,c,d e,f g, h, i, j, k, l m,n 1, 2, 3, 4, 5, 6 1, 3, 4, 5, 6, 7 the output should be a,b,c,de,f g, h, i, j, k, lm,n 1, 2, 3, 4,5, 6 1, 3, 4, 5, 6, 7 (4 Replies)
Discussion started by: ratheeshjulk
4 Replies

5. Shell Programming and Scripting

awk to replace second occurance

#original file . . ~ ~ Index=2 xxx replace #dont replace 1st occurance yyy Index=2 xxx replace #substitue replace with "REPLACE" yyy Index=2 xxx replace #substitue replace with "REPLACE" yyy Index=3 xxx replace (3 Replies)
Discussion started by: cjjoy
3 Replies

6. Shell Programming and Scripting

search and replace the last occurance of a match in a file

HI please let me know if there is any command to search and replace only the last occurence of a string in aline. Eg: " This cat is a cat with a tail longer of all cat." I need to replace only the last "cat" in the above line. thanks (3 Replies)
Discussion started by: harikris614
3 Replies

7. UNIX for Dummies Questions & Answers

Replace first 5 occurance of a pattern

There is scenario that i have to replace a pattern at the first 5 occurance in a file. say i need to replace 'abc' by 'xyz' at its first 5 occurance in a file a.txt, can anybody help me how it can be done, i can do complete replacement of the pattern using sed throughtout the file. (1 Reply)
Discussion started by: palash2k
1 Replies

8. UNIX for Dummies Questions & Answers

replace the n'th occurance in a file

Hi All, How can i replace the n'th occurance in a file. ? I have a property file like EAR;_TrackingEAR;META-INF/application.xml;xml;context-root;1;valeur EAR;_TrackingEAR;META-INF/application.xml;xml;context-root;2;valeur2... (2 Replies)
Discussion started by: subin_bala
2 Replies

9. UNIX for Dummies Questions & Answers

search for a word and it's occurance at the output

hey to everybody this is my first post at this forum I need a very immediate answer for this question. If you can, I will be delightfull I have a file called example.txt and I want to seek for the for hello and learn the number of the occurance of hello (2 Replies)
Discussion started by: legendofanatoli
2 Replies

10. UNIX for Dummies Questions & Answers

String search - Command to find second occurance

Hi, I am new to Unix world. Is there any command which can directly return the second occurance of a particular string in a file? Basically, I want to read the contents of the file from the second occurance of a particualr string. Can be implemented using a loop, but am just wondering if there... (5 Replies)
Discussion started by: saurabhsinha23
5 Replies
Login or Register to Ask a Question