【问题标题】:Find related text from different file and update using sed or awk or grep从不同文件中查找相关文本并使用 sed 或 awk 或 grep 进行更新
【发布时间】:2020-01-20 17:41:31
【问题描述】:

我有两个文本文件。
1. user.txt 包含以下文字

ServerName:xyz  
Username:foo  
UserID:  

需要从不同的文件中获取用户 ID,比如 properties.txt 并在 user.txt 中更新它。

properties.txt:

ServerName,Username,UserID,  
abc,joe,123,  
jkl,brad,5678,  
xyz,foo,456,

任何帮助将不胜感激。我需要一个可以同时在 AIX、Linux 上运行的解决方案

谢谢,

【问题讨论】:

  • 请在帖子中添加您为解决自己的问题所做的努力,然后告诉我们。

标签: linux awk sed grep aix


【解决方案1】:

复杂的 sed 解决方案:

sed -e '/Username/h;/UserID/{x;s/.*:/printf "UserID:%d" $(grep /;s/$/ properties.txt | grep -oE [0-9]+)/e}' user.txt

这使用h 将用户名粘贴在保留空间中,然后使用x 在处理后续用户ID 行时取回该行。然后将用户名行转换为printf "UserID: %d" $(grep [Username] properties.txt | grep -oE [0-9]+),在shell 中使用s///e 执行

如果 shell 不支持 $( ... ) 语法,那么反引号可能会起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    相关资源
    最近更新 更多