replace first instance(not first instance in line)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace first instance(not first instance in line)
# 1  
Old 02-07-2008
replace first instance(not first instance in line)

Alright, I think I know what I am doing with sed(which probably means I don't). But I cant figure out how to replace just the first occurance of a string. I have tried sed, ed, and grep but can't seem to figure it out. If you have any suggestions I am open to anything!
# 2  
Old 02-07-2008
By "first occurance" do you mean first occurance in a line or first occurance in a file?
# 3  
Old 02-07-2008
Assuming it is the first occurance in a file, the following example should show you how to
do what you want to do using awk.

Code:
$ cat file
true line1
true line2
$ awk '/true/ && n == 0 { sub(/true/,"false"); ++n } { print }' file
false line1
true line2
$

# 4  
Old 02-07-2008
Thank you

You are awsome, THANKS!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace every second instance of delimeter

Hi, Need help on replacing every second instance of delimeter. Scenario: var="Name1,Value1,Name2,Value2,Name3,Value3,Name4,Value" I want every second "," to replace with "|" I tried like below echo $var| sed 's/,/|/2' But, it's not working. Expected output: ... (4 Replies)
Discussion started by: Sumanthsv
4 Replies

2. Shell Programming and Scripting

awk line instance counter

I Have a text file with several thousand lines of text. Occasionally there will be a "sysAlive" line of text (every so often) What would be an awk command to print every line of text, and to put in incrementing counter ONLY on the "sysAlive" lines For example: >cat file.txt lineAAA a b c d... (4 Replies)
Discussion started by: ajp7701
4 Replies

3. Shell Programming and Scripting

Script to replace last instance of . between two consecutive = sign by ,

Suppose you have a line like this: cn=user.blr.ou=blr.india.o=company The line should be converted like this: cn=user.blr,ou=blr.india,o=comapny Was wondering how to do that using shell script. Please use tags where appropriate, thank you (4 Replies)
Discussion started by: saurabhkoar
4 Replies

4. Shell Programming and Scripting

how to fgrep -f two files and get only one instance of each matched line

Hello, I have two files: file1 x v r g file2 aaaa,x,1111 bbbb,v,2222 bbbb,v, cccc,r,3333 dddd,s,4444 eeee,q,5555 ffff,p,6666 (12 Replies)
Discussion started by: smarones
12 Replies

5. Shell Programming and Scripting

Complex find and replace only 1st instance string with dynamic combination

test.txt is the dynamic file but some of combination are fix like below are the lines ;wonder_off = ;wonder_off = disabled wonder_off = wonder_off = disabled the test.txt can content them in any order #cat test.xt ;wonder_off = ;wonder_off = disabled wonder_off = wonder_off =... (5 Replies)
Discussion started by: SilvesterJ
5 Replies

6. Shell Programming and Scripting

Using sed can you specify the last instance of a character on a line?

I was told a way to do this with awk earlier today but is there a way with sed to specify the last instance of a character on a line? You will know what character you're looking for but there could be none or one hundred instances of it on a line say and you ONLY want to specify the last one for... (3 Replies)
Discussion started by: Bashingaway
3 Replies

7. Shell Programming and Scripting

Replace all but skip first instance in a line

I have a record like the one given below. 010000306551~IN ~N~ |WINDWARD PK|Alpharetta| If ~ is present more than instance in a line,then I need to delete those instances. Any ideas? I am working in Solaris (7 Replies)
Discussion started by: prasperl
7 Replies

8. Shell Programming and Scripting

Append instance count to each line

Hello forum, I need help with a script for displaying the number of instances/times a particular line appears in a tab-delimited file and append that number to the end of the line. Example text file: aaa bbb ccc ddd ggg hhh kkk nnn aaa bbb ccc ddd aaa bbb ccc ddd ppp qqq nnn sss ggg... (1 Reply)
Discussion started by: jaysean
1 Replies

9. Shell Programming and Scripting

replace nth instance of string

Hi all, I have file with following content ........................... ..........TEST.......... ..........TEST.......... ..................... .....TEST.......... ..................... ..................... .....TEST.......... I want to replace nth "TEST" with "OK" using... (4 Replies)
Discussion started by: uttamhoode
4 Replies

10. Shell Programming and Scripting

sed replace 2nd instance

Hello, I want to replace 2nd instance of "foo" in a file use sed. Any suggestions? (2 Replies)
Discussion started by: katrvu
2 Replies
Login or Register to Ask a Question