我需要在下面的“sometest,readtest,writetest”之前删除所有这些内容。
2019-12-09 10:04:34 +0000 Test (err): sometest: some : text server:::test
2019-12-09 10:04:34 +0000 Test (notice): readtest: some
2019-12-09 10:04:34 +0000 Test (info): writetest: some ::: text
预期输出:
sometest: some : text server:::test
sometest: some
sometest: some ::: text
答案1
和sed
:
sed 's/[^:]*:[^:]*:[^:]*: //' file
或者使用扩展正则表达式 (ERE):
sed -E 's/([^:]*:){3} //' file
代替
- 任何非
:
字符后跟:
(三次) - 后跟一个空格字符
带有一个空字符串。
输出:
sometest: some : text server:::test
readtest: some
writetest: some ::: text
答案2
sed
如果我们确定括号出现,您可以尝试使用:
sed 's/^.*): //' file
输出:
sometest: some : text server:::test
readtest: some
writetest: some ::: text
或者用剪切:
cut -d':' -f4- file | sed 's/^ //'
答案3
这是一个有趣的问题,因为您可以选择不同的方法来做到这一点。其中一些在面对或意外输入时或多或少是稳健的。您是否想要显式匹配 sometest、readtest、writetest 关键字以获得鲁棒性。或者只匹配任何第三个冒号。使用 cut 命令或 sed 或 awk 或 perl 或 . 。 。
通过指出日期/时间中需要忽略的冒号可以使问题更加清楚。
# after 3rd colon (but forgetting to strip the space after it)
sed s/[^:]*:[^:]*:[^:]*://
# this cut command does the same as that sed
cut -d: -f 4-
# A cut command delimiting on space might be better actually
# assuming two words always to be stripped "Test (something)"
# this cut command leaves out the whitespace as expected
cut -d" " -f6-
# after 3rd colon (strip leading space):
sed "s/[^:]*:[^:]*:[^:]*: *//"
# after 3rd colon (strip leading WHITEspace):
sed "s/[^:]*:[^:]*:[^:]*:\s*//"
# cut all those things explicitly before "sometest,readtest,writetest"
sed -E 's/.*(some|read|write)(test:)/\1\2/'
# exactly the same command works in perl
perl -pe 's/.*(some|read|write)(test:)/\1\2/'
您的预期输出看起来包含不正确的前导关键字。
sometest: some : text server:::test
readtest: some
writetest: some ::: text
最后,在您的示例和日志文件中,通常有时您希望删除时间戳,例如 cut 命令以保留“测试(某事)”位:
# A cut command to just strip the timestamp and leave in the "Test (something)"
cut -d" " -f4-