In Linux, how do I truncate command-line output?

If I grep -nr sumthin * in my source code directory, it also spews out very long lines from minified JavaScript or CSS files. I want to get just the first 80 characters per line.

For example, a regular grep gives me this:

css/style.css:21:   behavior: url("css/iepngfix.htc")
css/style-min.css:4:.arrow1{cursor:pointer;position:absolute;left:5px;bottom:10px;z-index:13;}.arrow2{cursor:pointer;position:absolute;right:5px;bottom:10px;z-index:13;}.calendarModule{z-index:100;}.calendarFooterContainer{height:25px;text-align:center;width:100%!important;z-index:15;position:relative;font-size:15px!important;padding:-2px 0 3px 0;clear:both!important;border-left:1px solid #CCC;border-right:1px  ... etc.

but I'd like to get just this instead:

css/style.css:21:   behavior: url("css/iepngfix.htc")
css/style-min.css:4:.arrow1{cursor:pointer;position:absolute;left:5px;bottom:

What Linux command can do this?


Solution 1:

OMG, I totally forgot about cut!

grep -nr sumthin * | cut -c -80

^ does the trick! >_<

Solution 2:

Other than cut you can use fold (and in some cases fmt).
fold is part of coreutils package.

$ echo "some very long long long text" | fold -w 5   # fold on 5 chars per line
some 
very 
long 
long 
long 
text

fold doesn't cut the remaining text, but outputs it on the next line.

Solution 3:

While not exactly what you want to do, you could use awk to print a certain number of columns. You can specify the delimiter to be ":" in this case.