我有一个 .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/