小编use*_*657的帖子

删除文件中的用户名并替换为增量数字

这是一个文本处理问题。我有2个文件:

joeblogs
johnsmith
chriscomp
Run Code Online (Sandbox Code Playgroud)
12:00:00 (AAA) OUT: "string" joeblogs@hostname
12:00:00 (AAA) OUT: "string" joeblogs@hostname
12:00:00 (AAA) OUT: "string" johnsmith@hostname
12:00:00 (AAA) OUT: "string" joeblogs@hostname
12:00:00 (AAA) OUT: "string" chriscomp@hostname
Run Code Online (Sandbox Code Playgroud)

文件 1 包含出现在日志中的唯一用户名列表(文件 2)。

期望输出

12:00:00 (AAA) OUT: "string" USER1@hostname
12:00:00 (AAA) OUT: "string" USER1@hostname
12:00:00 (AAA) OUT: "string" USER2@hostname
12:00:00 (AAA) OUT: "string" USER1@hostname
12:00:00 (AAA) OUT: "string" USER3@hostname
Run Code Online (Sandbox Code Playgroud)

我想我不需要这两个文件。文件 1 是通过解析文件 2 的唯一用户名生成的。我的逻辑是获取我知道出现在文件 2 中的用户名列表,并循环遍历它,替换为sed.

就像是:

for i in $(cat file1);do sed -e 's/$i/USER[X]';done
Run Code Online (Sandbox Code Playgroud)

USER[X]每个唯一的用户名递增。 …

grep sed awk text-processing

6
推荐指数
3
解决办法
697
查看次数

标签 统计

awk ×1

grep ×1

sed ×1

text-processing ×1