Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
"minified"? What does that mean? –  CarlF Sep 7 '11 at 17:25
    
    
Thanks. Correct link is en.wikipedia.org/wiki/Minification_%28programming%29 –  CarlF Sep 8 '11 at 14:37
    
Both links lead to the same page for me. –  Victor Mar 22 '13 at 10:04

3 Answers 3

up vote 32 down vote accepted

OMG, I totally forgot about cut!

grep -nr sumthin * | cut -c -80

^ does the trick! >_<

share|improve this answer
1  
That doesn't work if escape sequences or color codes are involved; take for example git log --oneline --graph --color=always | cut -c -$COLUMNS –  badp Jan 28 at 12:01
    
I get that it doesn't truncate at exactly 80 displayed characters, but it still solves my problem: none of the lines wrap, so you still only see the leading portion of each line (which is important in my specific use case, so I can see the <file>:<linenumber> output of grep -n). –  Nikki Erwin Ramirez Jan 29 at 5:13
    
Long commit comments also kinda "break" your sample command's output, if not truncated. git log --oneline --graph --color=always | more will show the graph interrupted by wrapped text. i.imgur.com/X8G7SIV.png –  Nikki Erwin Ramirez Jan 29 at 5:24
    
yeah, that's why I googled for this question to begin with :) –  badp Jan 29 at 8:24

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.

share|improve this answer
    
Thanks for this, this might be useful in the future. In my case, I really wanted to cut the text, so the file:linenumber prefix of the grep output are contiguous, for easy scanning by eye. –  Nikki Erwin Ramirez Sep 8 '11 at 4:06

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.

share|improve this answer
    
There isn't a specific delimiter, though. I'm really cutting the output at a specific length, to make it fit in 1 line in the terminal. –  Nikki Erwin Ramirez Sep 8 '11 at 4:03
    
@Nikki then cut is what you want. I am sure that you can do something clever with awk to get the same thing but really, cut is easier. ^_- –  Sardathrion Sep 8 '11 at 7:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.