用于删除一行的前 N ​​个字符的 unix 命令是什么?

发布于 2024-07-23 01:05:37 字数 164 浏览 7 评论 0原文

例如,我可能想:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

小猫一只 2024-07-30 01:05:37

使用剪切。 例如。 去除每行的前 4 个字符(即从第 5 个字符开始):

tail -f logfile | grep org.springframework | cut -c 5-

Use cut. Eg. to strip the first 4 characters of each line (i.e. start on the 5th char):

tail -f logfile | grep org.springframework | cut -c 5-
撑一把青伞 2024-07-30 01:05:37
sed 's/^.\{5\}//' logfile 

然后你用你想要的数字替换 5...它应该可以解决问题...

编辑
如果对于每一行

sed 's/^.\{5\}//g' logfile
sed 's/^.\{5\}//' logfile 

and you replace 5 by the number you want...it should do the trick...

EDIT
if for each line

sed 's/^.\{5\}//g' logfile
话少情深 2024-07-30 01:05:37

这是在 bash 中测试的简单函数。 函数的第一个参数是字符串,第二个参数是要删除的字符数

function stringStripNCharsFromStart {
    echo ${1:$2:${#1}}
}

用法

$ stringStripNCharsFromStart "12abcdefgh-" 2
# 2abcdefgh-

屏幕截图

控制台结果屏幕截图

Here is simple function, tested in bash. 1st param of function is string, 2nd param is number of characters to be stripped

function stringStripNCharsFromStart {
    echo ${1:$2:${#1}}
}

Usage:

$ stringStripNCharsFromStart "12abcdefgh-" 2
# 2abcdefgh-

Screenshot:

Screenshot of result from console

铁憨憨 2024-07-30 01:05:37

您可以使用 cut:

cut -c N- file.txt > new_file.txt

-c: 字符

file.txt: 输入文件

new_file.txt: 输出文件

< code>N-: 从 N 到末尾的字符将被剪切并输出到新文件中。

还可以有其他参数,例如: 'N' 、 'NM' 、 '-M' 分别表示第 n 个字符、第 n 到 m 个字符、第一个到第 m 个字符。

这将对输入文件的每一行执行操作。

You can use cut:

cut -c N- file.txt > new_file.txt

-c: characters

file.txt: input file

new_file.txt: output file

N-: 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.

无人问我粥可暖 2024-07-30 01:05:37
x=hello

echo ${x:1}

返回 ello

根据需要将 1 替换为 N

x=hello

echo ${x:1}

returns ello

replace 1 with N as required

Smile简单爱 2024-07-30 01:05:37
tail -f logfile | grep org.springframework | cut -c 900-

会删除前 900 个字符

cut 使用 900- 显示到行尾的第 900 个字符

,但是当我通过 grep 管道传输所有这些时,我什么也得不到

tail -f logfile | grep org.springframework | cut -c 900-

would remove the first 900 characters

cut uses 900- to show the 900th character to the end of the line

however when I pipe all of this through grep I don't get anything

悲歌长辞 2024-07-30 01:05:37

我认为 awk 将是最好的工具,因为它可以过滤并在过滤的行上执行必要的字符串操作函数:

tail -f logfile | awk '/org.springframework/ {print substr($0, 6)}'

tail -f logfile | awk '/org.springframework/ && sub(/^.{5}/,"",$0)'

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:

tail -f logfile | awk '/org.springframework/ {print substr($0, 6)}'

or

tail -f logfile | awk '/org.springframework/ && sub(/^.{5}/,"",$0)'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文