0

I work with linux/solaris machines and I need to add some rule in my ksh script:

Please advise how to match the password word in file, and delete the value password after the separator "=" with perl liner command

second it will delete only the first password!

For example

  more file  ( before delete the password )

  Password fkwf324ei23
  Password=fkwf324ei23
  Pass_word=fevme
  Secret_Password=vrev873662j
  Password=fkwf324ei23

.

   more file ( after delete the password )

   Password fkwf324ei23
   Password=
   Pass_word=fevme
   Secret_Password=vrev873662j
   Password=fkwf324ei23
1

Just replace Password= at a beginning of a line followed by anything with the string Password=:

perl -i~ -pe 's/^Password=.*/Password=/' file

Update

To only replace the first occurence, add a flag:

perl -i~ -pe '$changed = s/^\s*Password=.*/Password=/ unless $changed;' file
  • Just one remark Password can be some spaces from the begining ( so maybe need to remove the "^" ? – user58412 Feb 4 '14 at 14:37
  • 1
    @Eytan: Removing the ^ would match Secret_Password. That's not what you want. Just add "optional spaces": ^ *Password, or even ^\s*Password if there can be tabs and other whitespace, too. – choroba Feb 4 '14 at 14:56
  • +1. You can reduce that a bit: s/^\s*Password=\K.*// – glenn jackman Feb 4 '14 at 15:02
  • ok , please advice - what need to add in your syntax so it will delete only the first password , once he find the first it will delete it but no the others ( because file can contain other Password , and I want to delete only the first one in my file ) – user58412 Feb 4 '14 at 15:03
  • see my update in the question – user58412 Feb 4 '14 at 15:22
0

Since you just want to change the first occurence and keep the rest of the file you need some state here:

perl -i~ -pe 'BEGIN { $y=1 }; ($y) && (s/^Password=\K.*//) && ($y=0)' file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy