【发布时间】:2010-11-01 14:16:31
【问题描述】:
例如,我可能想:
tail -f logfile | grep org.springframework | <command to remove first N characters>
我在想tr 可能有能力做到这一点,但我不确定。
【问题讨论】:
标签: bash unix command truncate
例如,我可能想:
tail -f logfile | grep org.springframework | <command to remove first N characters>
我在想tr 可能有能力做到这一点,但我不确定。
【问题讨论】:
标签: bash unix command truncate
tail -f logfile | grep org.springframework | cut -c 900-
将删除前 900 个字符
cut 使用 900- 显示第 900 个字符到行尾
但是,当我通过 grep 管道所有这些时,我什么也得不到
【讨论】:
使用cut。例如。去除每行的前 4 个字符(即从第 5 个字符开始):
tail -f logfile | grep org.springframework | cut -c 5-
【讨论】:
grep --line-buffered "org.springframework 解决该问题。
sed 's/^.\{5\}//' logfile
然后将 5 替换为所需的数字...应该可以解决问题...
编辑
如果对于每一行
sed 's/^.\{5\}//g' logfile
【讨论】:
你可以使用cut:
cut -c N- file.txt > new_file.txt
-c: 个字符
file.txt:输入文件
new_file.txt: 输出文件
N-:从N到end的字符被剪切并输出到新文件中。
还可以有其他参数,如:'N'、'N-M'、'-M'分别表示第n个字符、第n个到第m个字符、第一个到第m个字符。
这将对输入文件的每一行执行操作。
【讨论】:
我认为awk 将是最好的工具,因为它可以过滤并在过滤后的行上执行必要的字符串操作功能:
tail -f logfile | awk '/org.springframework/ {print substr($0, 6)}'
或
tail -f logfile | awk '/org.springframework/ && sub(/^.{5}/,"",$0)'
【讨论】: