bash - 可变长度的 Sed 替换

标签 bash sed

我有一个 .txt 文件,每个新行的 [ ] 之间都标有日期和时间戳,然后是其他文本。我只想保留时间戳和名称之后的文本,那么如何使用 sed 替换在 bash 中对其进行编码? 时间戳和名称具有不同的字符长度。

所以输入是 -

[11/26/17, 9:08:01 AM] Sean: sample text sample text
[8/8/18, 10:54:23 AM] Keith: sample sample sample
[3/8/18, 6:40:25 AM] John Doe: more text more text more text
[12/8/18, 12:40:30 AM] Jane Doe: abcd 1234 text text

所需输出:

sample text sample text
sample sample sample
more text more text more text
abcd 1234 text text

最佳答案

解决方案一:以下 awk 解决方案可能会对您有所帮助。

awk '{sub(/.*: /,"")} 1'    Input_file

解决方案二:如果您想将输出保存到Input_file本身,那么以下内容可能会帮助您。

awk '{sub(/.*: /,"")} 1'  Input_file > temp_file && mv temp_file Input_file

解决方案 3: 如果您想备份 Input_file 并将输出保存到 Input_file 本身,那么以下内容可能会帮助您。

sed -i.bak 's/.*: //'   Input_file

关于bash - 可变长度的 Sed 替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49205158/

相关文章:

regex - 匹配时如何查找、匹配和替换行尾

linux - ASCII 文本浏览器与 ASCII 文本浏览器 curl

bash - Sed 范围并删除最后一个匹配行

sed - 由于sed,如何在FFmpeg输出中替换没有换行的回车?

sed - 如何使用 sed 将 yml 文件中的变量替换为 gitlab ci 管道变量

linux - 替换文件中的未知行linux命令

linux - 如何从文件[shell]中某些特定行的末尾删除\n?

bash - 替换每个命令行参数中的文本,并将其传递给另一个命令

c++ - 在列中添加连续条目

linux - 在文件中使用 sed 插入存储在变量中的多行