One more awk question!

Hello,
I have the following command that does 2 searches.
awk '{if ($0 ~ /STRING1/) {c++} }{if ( c == 2 ) {sub(/STRING1/,"NEWSTRING") } } { print }' FILE

How do I search up after the first search?

thanks

What do you mean search up? Do you want to search the file from bottom line to top line?

You could do something like (if you have the tac command - Linux should have)

tac my_file | awk '{do something}'

Either way, you're not going to be able to achieve this in the "same" awk statement as you've posted.

You can, however (if searching from top to bottom doesn't bother you), just add another action to the existing awk.

e.g.

awk '
{
  if ($0 ~ /STRING1/) {
     c++
  } 
}
{ 
  if ( c == 2 ) {
     sub(/STRING1/,"NEWSTRING") 
  } 
} 
# perform some other substitution
{ sub(/FOO/,"BAR") }

{ print }' FILE

Cheers
ZB

Thanks for the reply. This is what I'm trying to do.
File:
Password="{3DES}hhzJvUvg3X4="
Properties="user=BLAH

I would like to search for BLAH and then replace the password. The password is encrypted when the application startup.
Does this help?

Okay, so let's say we have an input file, passwd, containing

Password="{3DES}someotherpass="
Properties="user=BAR
Password="{3DES}hhzJvUvg3X4="
Properties="user=BLAH
Password="{3DES}someotherpass="
Properties="user=FOO

And you want to change the password for user BLAH

Then, the following code

#!/bin/ksh
line=`grep -n "user=BLAH" passwd | awk -vFS=':' '{print $1}'`
((line=line-1))
awk -vl=$line '
{ if ( NR == l ) {
   sub( /{3DES}hhzJvUvg3X4=/, "mynewpass" )
  }
  { print }
}' passwd

Would print the (I assume) desired output of

Password="{3DES}someotherpass="
Properties="user=BAR
Password="mynewpass"
Properties="user=BLAH
Password="{3DES}someotherpass="
Properties="user=FOO

Is this what you want?

Cheers
ZB

Thanks, That will work for me. I'll have to use some sort of wild cars, because the encryption changes whenever the application is started so I do not know what the encrypted password is.

Greatly appreciated.