如何在Linux shell脚本中搜索用户id并替换名称

发布于 2025-01-18 10:26:33 字数 514 浏览 0 评论 0原文

例如a = userId

user.txt内容

A1:Alex:Password1
A2:John:Password2

搜索a2用户ID,

echo -n "Enter user id: "
read userID

然后在我输入用户ID后替换John用户名为Alexander,我需要在user.txt中搜索,找到了用户ID 输入替换名称约翰。

人们可以帮忙做到这一点。我尝试了一些执行说明,但没有起作用。 我该如何在Shell Script Linux中进行操作?感谢

您的帮助 我从答案中找到了如何做到的方式。

echo -n "Enter user id: "
read userID
echo -n "Enter user name: "
read userName

sed -i 's/'$userID':\w*/'$userID':'$userName'/g' user.txt

For example A = UserID

user.txt content

A1:Alex:Password1
A2:John:Password2

Search A2 User ID and Replace John username as Alexander

echo -n "Enter user id: "
read userID

After i enter the user id, i need to search in user.txt, after found the user id
Enter the replace name John.

Could people please help with how to do it. I tried a few executing instructions, but didn't work.
How can i do it in Shell Script Linux ? Thanks

Thanks for helping
I found the way how to do it from answer.

echo -n "Enter user id: "
read userID
echo -n "Enter user name: "
read userName

sed -i 's/'$userID':\w*/'$userID':'$userName'/g' user.txt

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难得心□动 2025-01-25 10:26:33

添加到您的bash脚本sed命令如下,

#!/bin/bash
echo -n "Enter user id: "
read userID

sed -i 's/A2:\w*:/A2:'$userID':/g' user.txt

它将在原地更改user.txt文件

Add to your bash script sed command as follow

#!/bin/bash
echo -n "Enter user id: "
read userID

sed -i 's/A2:\w*:/A2:'$userID':/g' user.txt

It will change in-place user.txt file

ゝ杯具 2025-01-25 10:26:33

另一个简单的方法是使用awk。您可以将用户ID和替换名称从shell传递到awk,然后将记录找到第一个字段($ 1)与用户ID匹配并替换名称(3rd Five <代码> $ 3 )带有新的代码。

awk -v id="A2" -v nm="Alexander" -v OFS=":" -F: '$1==id {$3=nm}1' user.txt

字符串(或外壳变量)是通过-v = value参数进行awk变量的,其中id> id and nm nm 用于将用户ID和名称称为awk变量。 -v ofs =“:”awk输出字段分隔符设置为“:”,以便使用新名称修改记录后“:”被替换为分离器。 -f:告诉awk使用“:”作为字段分离器将记录(行)分开为字段。最后的1只是默认操作打印即可输出记录的速记。

示例使用/输出

使用Heredoc提供您可以做的输入:

$ awk -v id="A2" -v nm="Alexander" -v OFS=":" -F: '$1==id {$3=nm}1' << eof
> A1:Alex:Password1
> A2:John:Password2
> eof
A1:Alex:Password1
A2:John:Alexander

Another simple method is using awk. You can pass the userID and replacement name from the shell to awk and then locate the record where the first field ($1) matches the userID and replace the name (3rd field $3) with the new one.

awk -v id="A2" -v nm="Alexander" -v OFS=":" -F: '$1==id {$3=nm}1' user.txt

The strings (or shell variables) are made awk variables through the -v=value parameters where id and nm are used to hold the userID and name as awk variables. -v OFS=":" sets the awk Output Field Separator to ":" so that after the records is modified with the new name, the ":" are replaced as separators. The -F: tells awk to use ":" as the Field Separator to separate the record (line) into fields. The 1 at the end is just shorthand for the default operation print to output the records.

Example Use/Output

Using a heredoc to supply your input you can do:

$ awk -v id="A2" -v nm="Alexander" -v OFS=":" -F: '$1==id {$3=nm}1' << eof
> A1:Alex:Password1
> A2:John:Password2
> eof
A1:Alex:Password1
A2:John:Alexander

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文