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

What Linux command can do this?


OMG, I totally forgot about cut!

grep -nr sumthin * | cut -c -80

^ does the trick! >_<

  • 3
    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
  • 1
    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
  • 5
    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 '16 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

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

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

  • 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

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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