Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to truncate STDIN line length?

Tags:

truncate

perl

cut

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?

like image 402
Kyle Cronin Avatar asked Sep 26 '08 01:09

Kyle Cronin


People also ask

How do I truncate a string to 6 characters in Excel?

Select a blank cell next to the string you want to truncate, and enter this formula =LEFT(A1,6) (A1 is the string you use, and 6 indicates truncate the string into six characters), then drag fill handle over the cells which also need this formula.

How do you truncate text in CSS?

The text can also be truncated using the CSS ::after pseudo-element. In the next example, the overflow of the <span> is hidden, and the max-height is set based on the line-height . Example of truncating a multi-line text using the ::after pseudo-element: ¶

How do you put padding around truncated text?

If there is a need for spacing around the truncated text, padding must be applied to the parent element. The text can also be truncated using the CSS ::after pseudo-element. In the next example, the overflow of the <span> is hidden, and the max-height is set based on the line-height.

Can You trim a string to less than 10 code points?

By ignoring this, we might trim the string to fewer than 10 code points, or (worse) truncate it in the middle of a surrogate pair. On the other hand, String.length () is not a good measure of Unicode text length, so trimming based on that property may be the wrong thing to do.


1 Answers

Pipe output to:

cut -b 1-LIMIT

Where LIMIT is the desired line width.

like image 66
nobody Avatar answered Oct 03 '22 12:10

nobody