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:
- <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ef979797969696af828e8683c18c8082" rel="noreferrer noopener nofollow">[email protected]</a>
上面是我的配置文件。我正在尝试通过 shell 脚本更改此配置文件中的密码。 我使用的命令是
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
以下 cmd 将以 **password**
形式输出。
现在我需要将密码更改为同一管道中的另一个密码。
有人可以指导我吗?
预期输出:
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:
- <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8af2f2f2f3f3f3cae7ebe3e6a4e9e5e7" rel="noreferrer noopener nofollow">[email protected]</a>
最佳答案
您能否尝试仅使用所示示例进行以下编写和测试。 OP 提到,MESSLY
可能有多个部分,并且只应编辑其中包含 my_homedir
的部分。此处使用 tac
+ awk
组合。根据专家的建议,应该使用像yq
这样的工具,如果OP没有能力安装它,那么可以使用它,但不能保证它可以工作像yq
这样的工具code>,这仅按照所示示例编写。
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
关于linux - 使用单个 Linux 管道查找和更改密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64604207/