linux - 有人可以向我解释这些 "sed"命令以及它们在做什么吗?

标签 linux bash sed sh

谁能快速解释一下这些线的作用以及它们的作用是什么?

1) sed -i "/^exampleusername/s/:1000:1000:/:${UID}:${GID}:/g"/etc/passwd

2) sed -i "/^examplegroupname/s/:1000:/:${GID}:/g"/etc/group

我看到了他们here如果您对更多上下文感兴趣。

最佳答案

您应该查看 sed 教程或手册页。这是 #1 分解的(并且不再有效的 sed)。第二个具有相同的一般形式:

/^exampleusername/      # Perform a command on matching lines (address)
    s/                  # Substitute command with / as delimiter
      :1000:1000:       # Pattern to find
      /                 # Delimiter
      :${UID}:${GID}:   # String to replace with (expanded by bash)
      /                 # Terminating delimiter
      g                 # Regex flag (global: replace all matches in line)

所以它只是找到以 exampleusername 开头的行,并将所有出现的 :1000:1000: 替换为您当前用户的 UID/GID。

这有效地使 exampleusername 与您当前的用户相同。

关于linux - 有人可以向我解释这些 "sed"命令以及它们在做什么吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49833264/

相关文章:

linux - 在 linux devicetree (arm) 中定义 3 个由相同 GPIO 引脚控制的调节器

c++ - 如何在 bazel/googletest 中使用环境变量

bash - 如何在 shell 脚本中单行获取 grep 的输出?

regex - 使用纯 sed 对列进行复杂转置

linux - 在 Linux 中使用字典查找和替换单词

sed - 以最简单的方式添加注释字符

c - 耳机插3.5接口(interface)怎么知道?

C 程序链接错误 : what is causing these?

linux - 流式传输文件内容直到遇到子字符串

python - 来自 Linux 上的命令队列的并行处理(bash、python、ruby ......随便)