7

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?

double-beep
  • 5,031
  • 17
  • 33
  • 41
Kyle Cronin
  • 77,653
  • 43
  • 148
  • 164

9 Answers9

12

Pipe output to:

cut -b 1-LIMIT

Where LIMIT is the desired line width.

nobody
  • 19,814
  • 17
  • 56
  • 77
11

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.

nobody
  • 19,814
  • 17
  • 56
  • 77
2

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.

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
Yanick
  • 1,250
  • 8
  • 9
0

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
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
  • 1
    This is basically the opposite of what I was looking for. When viewing lines of a log file for example, it's annoying when the text is wrapped onto multiple lines. The purpose of this question was to determine if there was a way to truncate the output lines so that there wouldn't be any wrapping. From your example, it seems that fold's task is to *cause* the wrapping of lines. – Kyle Cronin Oct 15 '12 at 16:41
  • Ah, I thought you were just unhappy with the way the lines wrapped not the fact that they wrapped. Then the cut solution is the right one and you can use eval $(ttype -s) to automatically set the COLUMNS variable for the cut width if you like. – Ed Morton Oct 17 '12 at 19:51
0

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.)

ysth
  • 96,171
  • 6
  • 121
  • 214
0

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

typeset -L70 line
while read line
do
  print $line
done
runrig
  • 6,486
  • 2
  • 27
  • 44
0

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";
}
dland
  • 4,319
  • 6
  • 36
  • 60
0

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.

Sam Martin
  • 1,030
  • 1
  • 8
  • 16
0
use strict;
use warnings
use String::FixedLen;

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

while (defined($str = <>)) {
    chomp;
    print "$str\n";
}
Jason Plank
  • 2,336
  • 5
  • 31
  • 40