1

我有一个具有这种格式的文件:

21:26:54.21 PT  disconnected
21:26:56.18 CC1 received TCMTM
21:26:56.18 CC2 received TCMTM
21:26:56.18 TFT received TCMTM
21:26:56.18 FEC received TCMTM

我想删除每一行的第一部分(21:26:54.21),我试过这个:

sed "s/^[:digit:]*[:space:]//"  file.log

但它不起作用,我得到与条目相同的输出。

4

4 回答 4

5

cut

$ cut -d' ' -f2- file
PT  disconnected
CC1 received TCMTM
CC2 received TCMTM
TFT received TCMTM
FEC received TCMTM

将空格设置为分隔符并从字段 2 打印到最后一个 ( f2-)。

awk

$ awk '{$1=""}1' file  ## leading space :(
 PT disconnected
 CC1 received TCMTM
 CC2 received TCMTM
 TFT received TCMTM
 FEC received TCMTM

将第一个字段设置为空。

$ awk '{for (i=1;i<=NF; i++) $i=$(i+1); NF=NF-1}1' file
PT disconnected
CC1 received TCMTM
CC2 received TCMTM
TFT received TCMTM
FEC received TCMTM

它将字段编号减一,将每个字段移到前一个字段编号(因此,第一个被删除)。

sed

$ sed 's/^[^ ]* //g' file
PT  disconnected
CC1 received TCMTM
CC2 received TCMTM
TFT received TCMTM
FEC received TCMTM

删除从行首到第一个空格的所有内容。

或基于您的sed

$ sed "s/^[0-9:.]* //g" file
PT  disconnected
CC1 received TCMTM
CC2 received TCMTM
TFT received TCMTM
FEC received TCMTM

删除从行首到第一个空格的所有字符0-9:或。.

于 2013-11-05T10:52:53.377 回答
2

使用awk

awk '{$1=x;sub(/^ /,x)}1' file
PT disconnected
CC1 received TCMTM
CC2 received TCMTM
TFT received TCMTM
FEC received TCMTM

另一个版本

awk '{sub(/^[0-9:.]+ /,x)}1' file

还有一个

awk '{sub(/[^ ]* /,x)}1' file
于 2013-11-05T10:57:28.423 回答
0

除了缺少时间戳中需要匹配的:and之外,您在使用字符类的方式上还有一个小错误:.

sed "s/^[[:digit:]:.]*[[:space:]]//" file.log

诸如[:digit:]仅在内部有效的字符类[...],因此您需要

s/^[[:digit:]]*

匹配行首的零个或多个数字。您仍然需要匹配:or .,因此也将它们添加到[...]

s/^[[:digit:].:]*

认为[:digit:]是替代品0-9,不是[0-9]。你可以写上面的

s/^[0-9.:]*

这将匹配数字 0、1、2、3、4、5、6、7、8、9,但不匹配在当前语言环境中可能被视为数字的任何其他字符。

于 2013-11-05T14:19:05.943 回答
0

使用 sed:

sed 's/^[0-9:.]* *//' file.log

输出:

PT  disconnected
CC1 received TCMTM
CC2 received TCMTM
TFT received TCMTM
FEC received TCMTM
于 2013-11-05T11:05:35.883 回答