Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 64 down vote accepted

OMG, I totally forgot about cut!

grep -nr sumthin * | cut -c -80

^ does the trick! >_<

share|improve this answer
2  
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 '14 at 12:01
1  
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 '14 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 '14 at 5:24
    
yeah, that's why I googled for this question to begin with :) – badp Jan 29 '14 at 8:24
2  
If the purpose is just to be able to visually see things well(not redirecting the output somewhere for use) then less -S is what you want. -S is --chop-long-lines. Basically, it turns off line wrapping – entropy May 5 at 11:12

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .