Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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

I need to view large logs files using a bash shell. I was using less to open the files, but since the lines are too lengthy there is some kind of line/word wrapping going on.

Since the files are Log4J logs, and there is a pattern in the beginning of each line, having lines wrapped makes it difficult to analyze the output, so I started using less -S which chops long lines.

But now I need to use tail -f, and it also line wraps the output. Is it possible to disable line wrap in a bash shell for all the commands?

Note: there is an answer to a different question that mentions the escape sequence echo -ne '\e[?7l', but it seems to not work on bash.

share|improve this question
2  
Note that this is unrelated to your choice of shell (bash), but it can depend on your choice of terminal (xterm, Terminal, gnome-terminal, konsole, …). See What is the exact difference between a 'terminal', a 'shell', a 'tty' and a 'console'? – Gilles Sep 11 '11 at 18:16
2  
As Gilles mentioned, it is a matter of the terminal emulator. For example, in screen, there is a "wrap" command. In xterm, there is a -aw option to prevent automatic line wraps. Likely there are some X resources for other terminal emulators. What emulator are you using? – Arcege Sep 12 '11 at 20:04
    
I am using gnome-terminal. – nunaxe Sep 17 '11 at 8:53
    
@Arcege: Actually, it is +aw for xterm :) But, this solves the issue for output (e.g., on ls -l with a bunch on long Windows-ish names from TPB, it works fine), for input, when entering a command, you probably still want wrap (or, at least you don't want it the way it is now on +aw: try write beyond the "column border" and then backtrack for a huge GFX bug). – Emanuel Berg Nov 9 '12 at 23:37
    
By the way, it seems neither urxvt nor rxvt can disable line wraps. – Emanuel Berg Nov 9 '12 at 23:50
up vote 4 down vote accepted

Do you actually need tail -f or would something like less +F do? Since it sounds like you still want an interactive pager, it seems to me it would be much easier to stick with less than to reimplement one yourself.

A final note: have you considered tail -f file | less?

share|improve this answer
    
I made some experiences with screen and xterm as those emulators have options to prevent line wraps. But there is a big difference between truncating the line and hiding the tail of the line so we can view it if we want. Using tail -f file | less -S is not perfect as it seems to disable the -f but it is the best solution I found. – nunaxe Sep 17 '11 at 8:56

Try:

less -S +F filename

  • Press Ctrlc to stop tailing and now You can move left and right using cursor keys.
  • Press Shiftf to resume tailing
  • Press Ctrlc, q to quit
share|improve this answer

Supposing you have the COLUMNS variable defined, you can execute

tail -f your-app.log | cut -c -$COLUMNS

otherwise substitute $COLUMNS with the columns width of the terminal, as obtained by stty -a.

share|improve this answer
2  
Another alternative to $COLUMNS (is not immediately updated on SIGWINCH, only on the next prompt) and stty -a (harder to use in script) is tput cols. – manatwork Sep 11 '11 at 12:43
    
Thanks enzotib. That works, but it trims the line and we are unable to see the end of the long lines. Is is possible to make cut behave like less -S where the long line are buffered and we are able to see the complete line using the directional keys? – nunaxe Sep 11 '11 at 15:12
    
Uhm, not that simple. I'm thinking of wired solutions as e.g. two separate screen windows each with a tail -f | cut or a script emulating in some way the less behavior. But do not have a solution, at the moment. – enzotib Sep 11 '11 at 16:16
1  
Side-note: cut only counts bytes; unlike less -S, it's going to screw up on coloured text, or anything with ANSI escapes. Might screw up Unicode too. – ELLIOTTCABLE Jun 8 '13 at 20:39
    
Yes, less -S is more useful with colored text. You can use less -S -E to exit immediately - useful for cutting colored output at $COLUMNS. – blueyed May 13 '15 at 16:42

The terminator (http://software.jessies.org/terminator/) terminal emulator allow to not wrap long lines and has horizontal scrolling (but is written in Java).

share|improve this answer
    
How do I enable it? It doesn't seem to work by default. – Artem Ice Aug 29 '12 at 13:26
    
As of this writing, horizontal scroll is on by default. Great, cross-platform recommendation. Thanks! – user7089 Jan 26 '13 at 0:18

Two good answers/examples here

http://www.commandlinefu.com/commands/view/1710/tail-a-log-file-with-long-lines-truncated

tail -f logfile.log | cut -b 1-80

tail -f logfile.log | cut -b -$(tput cols)

One caveat: at least on the built in terminal on my Mac cut does not seem to handle tab characters very well. It seems it displays them with e.g., 8 spaces but just calculates them as 4 space wide or something like that. In other words, if your terminal is 80 characters wide and your output contains several tabs per line you must set the width to maybe 60 or something like that. YMMV.

share|improve this answer

Use below options with less. It will disable word wrap and preserve text colors, if specified.

less -SR +F filename

share|improve this answer

There are a lot of comments which stray from the question. OP's question was

But now I need to use tail -f, and it also line wraps the output. Is it possible to disable line wrap in a bash shell for all the commands?

Some comments were made about the autowrap feature, stating that not all terminals have it. Perhaps. But (aside from terminator, whose developers document no terminal description) all of the examples given were xterm, rxvt and some look-alike or descendent. Those are all related.

The +aw option in xterm corresponds to the autoWrap resource. Consulting the manual, it says that

   autoWrap (class AutoWrap)
           Specifies  whether  or  not  auto-wraparound should be enabled.
           This is the same as the VT102 DECAWM.  The default is "true".

and in XTerm Control Sequences it says

CSI ? Pm h
          ...
          DEC Private Mode Set (DECSET).
            Ps = 7  -> Wraparound Mode (DECAWM).

which certainly does not "depend on your choice of terminal", since any terminal with VT100-compatibility supports the feature. xterm and rxvt do this, for example. The others do as well.

Whether the feature would be useful to OP is debatable. Suppressing line-wrapping is only one aspect of the problem:

  • The shell knows the width of the terminal—but that can be overridden by setting COLUMNS to a "large" value.
  • Of course that means that applications will spend a lot of time writing on the right-margin (and some, getting it wrong, will start a new line anyway).
  • OP probably assumed that the application would scroll left/right to make the wide terminal usable. (terminator does this — partly — but its other drawbacks cancel that out, except for those who only use the terminal for cat'ing a logfile to the screen).
  • what OP actually is looking for is a set of tools that can be told to disable line-wrap, especially for viewing logfiles. If the terminal works well enough for general use, it is irrelevant to the choice of tools which one uses within the terminal.

There are pagers which can do what is needed, e.g., multitail which lists in its features

Line wrapping can be switched off, after that one can scroll to the left/right with the cursor keys

Being ncurses-based, it should work on any of the cited terminals.

share|improve this answer

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.