用于删除一行的前 N 个字符的 unix 命令是什么?
例如,我可能想:
tail -f logfile | grep org.springframework | <command to remove first N characters>
我认为 tr 可能有能力做到这一点,但我不确定。
For example, I might want to:
tail -f logfile | grep org.springframework | <command to remove first N characters>
I was thinking that tr
might have the ability to do this but I'm not sure.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
使用
剪切
。 例如。 去除每行的前 4 个字符(即从第 5 个字符开始):Use
cut
. Eg. to strip the first 4 characters of each line (i.e. start on the 5th char):然后你用你想要的数字替换 5...它应该可以解决问题...
编辑
如果对于每一行
and you replace 5 by the number you want...it should do the trick...
EDIT
if for each line
这是在 bash 中测试的简单函数。 函数的第一个参数是字符串,第二个参数是要删除的字符数
用法:
屏幕截图:
Here is simple function, tested in bash. 1st param of function is string, 2nd param is number of characters to be stripped
Usage:
Screenshot:
您可以使用
cut
:-c:
字符file.txt:
输入文件new_file.txt:
输出文件< code>N-: 从 N 到末尾的字符将被剪切并输出到新文件中。
还可以有其他参数,例如: 'N' 、 'NM' 、 '-M' 分别表示第 n 个字符、第 n 到 m 个字符、第一个到第 m 个字符。
这将对输入文件的每一行执行操作。
You can use
cut
:-c:
charactersfile.txt:
input filenew_file.txt:
output fileN-:
Characters from N to end to be cut and output to the new file.Can also have other args like: 'N' , 'N-M', '-M' meaning nth character, nth to mth character, first to mth character respectively.
This will perform the operation to each line of the input file.
返回 ello
根据需要将 1 替换为 N
returns ello
replace 1 with N as required
会删除前 900 个字符
cut
使用 900- 显示到行尾的第 900 个字符,但是当我通过 grep 管道传输所有这些时,我什么也得不到
would remove the first 900 characters
cut
uses 900- to show the 900th character to the end of the linehowever when I pipe all of this through grep I don't get anything
我认为 awk 将是最好的工具,因为它可以过滤并在过滤的行上执行必要的字符串操作函数:
或
I think
awk
would be the best tool for this as it can both filter and perform the necessary string manipulation functions on filtered lines:or