Sed on first instance only

Hi, I've been trying to figure this one out and found a post about this on the forum here but the solution didn't seem to work for me. Basically what I have is a file that looks something like:

stuff
morestuff
0
otherthing
0
etc

I want to substitute for the 0 but what I want to substitute will be different for each one. Any ideas how to substitute 1 at a time with sed? Keep in mind that 0 is just the value of the variable I'll be using. I've been trying something like this with no luck...

sed s/$var/$value/1 file

Can you post a sample input as well as output....

Sure, so here would be my text file before. Let's just say I have a file of serial numbers:

E1111111
E2222222
E3333333
0
E5555555
0
E7777777

So the 0 are mess ups in the file and I want to replace each with different serial numbers E4444444 and E6666666 respectively so the output would be:

E1111111
E2222222
E3333333
E4444444
E5555555
E6666666
E7777777

Unfortunately when I use sed it does the whole file so the second 0 gets replaced by E4444444 before I can even try to substitute the E6666666

To replace the first instance in your file you can use this:

awk 'c && /^0$/{$0=r;c--}1' c=1 r="E4444444" file > newfile

To replace 2 values:

awk 'BEGIN{a[2]="E4444444";a[1]="E6666666"}
i && /^0$/{$0=a[i--]}1' i=2 file > newfile

Regards

is that just a hypothetical sample OR is that the real math you want to perform to derive ANY missing rows from the previous/non-missing one?

sed might not be the best tool here. However, GNU sed might work for you. Put all your substitutions, one line each, in "file2". And run sed on file1. Note, the newline in the middle of the sed script is necessary:

sed '/^0${ R file2
;d;}' file1

I have a gut feeling that the OP cannot predict what rows will be 'missing'. So gut is telling me the OP wants to derive the value of the missing row from the previous row value. In which case we'll have to know if the sample file is representative of the 'real life' data and what (if any) assumptions can be made regarding the data itself.

Once again, this is all guesses on my part....

Hi, so thanks for the input everyone. To clarify, in my input file the 0 is just an example of the data I want to replace, this can be text or other numbers or a combination of both. I have a loop running to replace everything so basically I will have $var which is the string to look for and $replace which is the replacement for it. Since it's in a loop the values of each variable will be changed with a new iteration. The issue I had in my script originally was that I was using sed and that replaced later values of $var when it shouldn't be so I wanted to limit it to just the first instance since my script will read the file top to bottom.

In any case, it sounds like sed isn't the best method. I tried the awk from above replacing what I thought to be my variables but no luck. I tried the following:

awk 'c && /^$var$/{$0=r;c--}1' c=1 r="$replace" file > newfile

awk 'c && /$0==s/{$0=r;c--}1' c=1 r="$replace" s="$var" file > newfile

Regards

looks like job for ksh:

my_serial=123

cat file |
while read junk ; do

  if [ $junk -eq 0 ]; then
    my_serial=$(( my_serial + 1 ))
    echo $my_serial
    continue
  fi

  echo $junk

done