如何截断 STDIN 行长度?

发布于 2024-07-06 21:41:21 字数 431 浏览 7 评论 0原文

我一直在解析一些日志文件,发现有些行太长,无法在一行上显示,因此 Terminal.app 会善意地将它们包装到下一行。 但是,我一直在寻找一种在一定数量的字符之后截断一行的方法,以便终端不会换行,从而更容易发现模式。

我编写了一个小的 Perl 脚本来执行此操作:

#!/usr/bin/perl

die("need max length\n") unless $#ARGV == 0;

while (<STDIN>)
{
    $_ = substr($_, 0, $ARGV[0]);
    chomp($_);
    print "$_\n";
}

但我有一种感觉,此功能可能内置于其他一些工具(sed?)中,我只是不太了解如何用于此任务。

所以我的问题有点相反:如何在不编写程序的情况下截断一行标准输入?

I've been parsing through some log files and I've found that some of the lines are too long to display on one line so Terminal.app kindly wraps them onto the next line. However, I've been looking for a way to truncate a line after a certain number of characters so that Terminal doesn't wrap, making it much easier to spot patterns.

I wrote a small Perl script to do this:

#!/usr/bin/perl

die("need max length\n") unless $#ARGV == 0;

while (<STDIN>)
{
    $_ = substr($_, 0, $ARGV[0]);
    chomp($_);
    print "$_\n";
}

But I have a feeling that this functionality is probably built into some other tools (sed?) That I just don't know enough about to use for this task.

So my question sort of a reverse question: how do I truncate a line of stdin Without writing a program to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(9

神经暖 2024-07-13 21:41:21

通过管道输出到:

cut -b 1-LIMIT

其中 LIMIT 是所需的线宽。

Pipe output to:

cut -b 1-LIMIT

Where LIMIT is the desired line width.

巴黎盛开的樱花 2024-07-13 21:41:21

我用于查看包含很长行的日志文件的另一种策略是将文件通过管道传输到“less -S”。 less 的 -S 选项将打印不换行的行,并且您可以通过按向右箭头键查看长行的隐藏部分。

Another tactic I use for viewing log files with very long lines is to pipe the file to "less -S". The -S option for less will print lines without wrapping, and you can view the hidden part of long lines by pressing the right-arrow key.

聊慰 2024-07-13 21:41:21

不完全回答这个问题,但如果您想坚持使用 Perl 并使用单行代码,一种可能性是:

$ perl -pe's/(?<=.{25}).*//' filename

其中 25 是所需的行长度。

Not exactly answering the question, but if you want to stick with Perl and use a one-liner, a possibility is:

$ perl -pe's/(?<=.{25}).*//' filename

where 25 is the desired line length.

末骤雨初歇 2024-07-13 21:41:21

通常的方法是

perl -wlne'print substr($_,0,80)'

Golfed(对于 5.10):(

perl -nE'say/(.{0,80})/'

不要将其视为编程,将其视为使用具有大量选项的命令行工具。)(是的, python 参考是有意的。)

The usual way to do this would be

perl -wlne'print substr($_,0,80)'

Golfed (for 5.10):

perl -nE'say/(.{0,80})/'

(Don't think of it as programming, think of it as using a command line tool with a huge number of options.) (Yes, the python reference is intentional.)

哀由 2024-07-13 21:41:21

Korn shell 解决方案(截断为 70 个字符 - 但易于参数化):

typeset -L70 line
while read line
do
  print $line
done

A Korn shell solution (truncating to 70 chars - easy to parameterize though):

typeset -L70 line
while read line
do
  print $line
done
荆棘i 2024-07-13 21:41:21

您可以使用绑定变量将其内容剪辑为固定长度:

#! /usr/bin/perl -w

use strict;
use warnings
use String::FixedLen;

tie my $str, 'String::FixedLen', 4;

while (defined($str = <>)) {
    chomp;
    print "$str\n";
}

You can use a tied variable that clips its contents to a fixed length:

#! /usr/bin/perl -w

use strict;
use warnings
use String::FixedLen;

tie my $str, 'String::FixedLen', 4;

while (defined($str = <>)) {
    chomp;
    print "$str\n";
}
神经大条 2024-07-13 21:41:21

这并不完全是您所要求的,但是 GNU Screen (包含在操作系统中) X(如果我没记错的话,在其他 *nix 系统上很常见)可以让您打开/关闭换行(Car 和 Ca Cr)。 这样,您可以简单地调整终端的大小,而不用通过脚本传递内容。

Screen 基本上为您提供了一个顶级终端应用程序中的“虚拟”终端。

This isn't exactly what you're asking for, but GNU Screen (included with OS X, if I recall correctly, and common on other *nix systems) lets you turn line wrapping on/off (C-a r and C-a C-r). That way, you can simply resize your terminal instead of piping stuff through a script.

Screen basically gives you "virtual" terminals within one toplevel terminal application.

千柳 2024-07-13 21:41:21
use strict;
use warnings
use String::FixedLen;

tie my $str, 'String::FixedLen', 4;

while (defined($str = <>)) {
    chomp;
    print "$str\n";
}
use strict;
use warnings
use String::FixedLen;

tie my $str, 'String::FixedLen', 4;

while (defined($str = <>)) {
    chomp;
    print "$str\n";
}
挖鼻大婶 2024-07-13 21:41:21

除非我没有抓住要点,UNIX 的“fold”命令就是为了做到这一点而设计的:

$ cat file
the quick brown fox jumped over the lazy dog's back

$ fold -w20 file
the quick brown fox
jumped over the lazy
 dog's back

$ fold -w10 file
the quick
brown fox
jumped ove
r the lazy
 dog's bac
k

$ fold -s -w10 file
the quick
brown fox
jumped
over the
lazy
dog's back

Unless I'm missing the point, the UNIX "fold" command was designed to do exactly that:

$ cat file
the quick brown fox jumped over the lazy dog's back

$ fold -w20 file
the quick brown fox
jumped over the lazy
 dog's back

$ fold -w10 file
the quick
brown fox
jumped ove
r the lazy
 dog's bac
k

$ fold -s -w10 file
the quick
brown fox
jumped
over the
lazy
dog's back
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文