2

When I ssh to my Linux servers and use grep like this:

grep 'timeout exceeded' logfile | less

word wrap does not work.

However, if I use the same command but use less first, like this:

less logfile | grep 'timeout exceeded'

the lines wrap. I am not sure what the problem is or if this is normal or not. But it happens regardless of the ssh client I use. I have tried both putty and an Ubuntu client. How can I fix this?

2

When you do

grep 'timeout exceeded' logfile | less

You can use the right arrow keys to move to the end of the line (left to move back).

  • Thanks. While that works and gives me the ability to see the whole line, it does not answer my question directly. Surely there is a way to force grep to word wrap? – user53029 Jul 27 '15 at 22:34
  • I couldn't find a way to force grep to do it, but the *NIX utility fold seems to do it okay: grep 'timeout exceeded' logfile | fold -s -w 150 | less The -s tells it to break at spaces; -w COLUMNS specifies the column width. – jackhamm Jul 27 '15 at 22:38
  • That is so odd. I thought I was just overlooking a simple grep flag or something. Well anyway fold works fine with the arguments you provided and does accomplish what I was after. Appreciate the help! – user53029 Jul 27 '15 at 22:51
  • 1) the rightmost command in a pipe is the one displaying on your screen. The left command outputs to the right command. 2) For less, "folding" is the default, unless you specify the -S or --chop-long-lines option. If you did not write those options in your command then check alias to see if your shell has an alias that adds it, or echo $LESS to see if you have the option in your default command environment variable. – DerfK Jul 27 '15 at 22:59
  • @DerfK - I wonder if something is set odd in my environment also; I was seeing the same behavior as OP with less not folding; that's why I added the fold to the command. – jackhamm Jul 27 '15 at 23:01
5

This is not the default behavior of less. The default is to wrap long lines.

You are seeing this behavior because you have the -S option (and several others) set in your LESS environment variable.

       -S or --chop-long-lines
              Causes  lines  longer than the screen width to be chopped (trun‐
              cated) rather than wrapped.  That is, the portion of a long line
              that does not fit in the screen width is not shown.  The default
              is to wrap long lines; that is, display  the  remainder  on  the
              next line.

To resolve the problem, check your shell startup scripts (e.g. $HOME/.bash_profile, $HOME/.bashrc) and the system shell startup scripts (e.g. those in the /etc/profile.d directory) to see where the environment variable is being set, and make the desired changes.

0

If your less is defaulting to not wrapping lines, try using the full path to less. Run which less and see what path it gives you. Then use that path instead. For example if it's at /usr/bin/less then try grep 'timeout exceeded' logfile | /usr/bin/less.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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