Find and change password using single linux pipe

Viewed 57
MESSLY:
  mail address: ubuntu://basics:password@host:1521/oracle
  directory_name: common setup
  directory: /u11/dodge
  mail_server: mailnetwork
  my_homedir: /home/lucifer
  logic_path:
  error_path:
  mail_id:
     - xxxyyy@mail.com

Above is my config file. I am trying to change the password in this config file through shell script. The cmd I'm using is

cat file_name.txt | grep "my_homedir" | grep -v "#" | awk '{FS=":";print $2}' |grep -B4 -w "$my_homedir" file_name.txt  | grep -o -P '(?<=//).*(?=@)' | cut -d ":" -f2

The following cmd will give output as **password**. Now I need to change the password to another password In the same pipe.

Could someone please guide me?

expected output :

MESSLY:
  mail address: ubuntu://basics:new_passwd@host:1521/oracle
  directory_name: common setup
  directory: /u11/dodge
  mail_server: mailnetwork
  my_homedir: /home/lucifer
  logic_path:
  error_path:
  mail_id:
     - xxxyyy@mail.com
2 Answers

Could you please try following, written and tested with shown samples only. Where OP has mentioned that there could be multiple sections of MESSLY and only those sections should be edited which have my_homedir in it. Using tac + awk combinations here. As per experts advise tools like yq should be used, if OP doesn't have capability to install it then this could be used but it can't be guaranteed to work tools like yq, this is written as per shown samples only.

tac Input_file | 
awk -v newPass="new_passwd" '
/^MESSLY/   { found="" }
/my_homedir/{ found=1  }
found && /mail address:/{
   match($0,/.*:\/\/[^:]*:/)
   first=substr($0,RSTART,RLENGTH)
   last=substr($0,RSTART+RLENGTH)
   sub(/.*@/,newPass"@",last)
   print first last
   first=last=""
   next
}
1' | 
tac

Let's say you have this input:

cat file.yml

MESSLY:
  mail address: ubuntu://basics:passwd@host:1521/oracle
  directory_name: common setup
  directory: /u11/dodge
  mail_server: mailnetwork
  my_homedir: /home/lucifer
  logic_path:
  error_path:
  mail_id:
     - xxxyyy@mail.com

MESSLY:
  mail address: ubuntu://basics:passwd@host:1521/oracle
  directory_name: common setup
  directory: /u11/dodge
  mail_server: mailnetwork
  my_homedir: /home/abcd
  logic_path:
  error_path:
  mail_id:
     - xxxyyy@mail.com

You may then use this awk:

awk -v RS= -v d='/home/lucifer' -v pass='new_password' '
$0 ~ d && match($0, /[[:space:]]mail address:[^\n]+/) {
   $0 = substr($0, 1, RSTART-1) \
   gensub(/^(([^:]*:){3})[^@]+(@.*)/, "\\1" pass "\\2", "1", substr($0, RSTART, RLENGTH)) \
   substr($0, RSTART+RLENGTH)
}
{
   ORS=RT
} 1' file.yml
MESSLY:
  mail address: ubuntu://basics:new_password//basics:
  directory_name: common setup
  directory: /u11/dodge
  mail_server: mailnetwork
  my_homedir: /home/lucifer
  logic_path:
  error_path:
  mail_id:
     - xxxyyy@mail.com

MESSLY:
  mail address: ubuntu://basics:passwd@host:1521/oracle
  directory_name: common setup
  directory: /u11/dodge
  mail_server: mailnetwork
  my_homedir: /home/abcd
  logic_path:
  error_path:
  mail_id:
     - xxxyyy@mail.com

However do consider using yq for manipulating yaml files.

Related