I track logs through tailf ( tail -f ). For convenience of display, I would like to add an empty line after each line.

For example:

 $ tailf /var/log/some.log log1 log2 log3 $ tailf /var/log/some.log | magic log1 log2 log3 

Tried through sed in two ways, but I receive the same without changes. The idea is to patch the line break and replace it with two lines. The general syntax is: sed 's/substitute_this/to_this/g'

Method 1: insert a line break as $'\n' .

 $ tailf /var/log/some.log | sed "s/$'\n'/test/g" log1 log2 log3 

It seems that $'\n' does not match line breaks in my log.

Method 2: insert a line break as a line break when entering the command:

 $ tailf /var/log/some.log | sed "s/ > /test/g" sed: -e expression #1, char 2: unterminated `s' command $ tailf /var/log/some.log | sed "s/\ /test/g" sed: -e expression #1, char 0: no previous regular expression 

If it were a shell script, one could use a hack :

 newline=' ' 

But I want to use it on the go. To make a function or alias is also not suitable, because I work with a large number of hosts and do not have the ability to individually configure .bashrc .

Actually, the question is: what should be in place magic ?

  • sed "s/$/\n/g" ? - BOPOH
  • @BOPOH sed: -e expression #1, char 8: unknown option to s' ` - Nick Volynkin ♦
  • I did not understand anything, everything worked as it should for me, maybe somehow I did something wrong? what distribution? I checked on ubunt. Those. This line gives me the right way: tailf /var/log/apache2/access.log | sed "s/$/\n/g" tailf /var/log/apache2/access.log | sed "s/$/\n/g" - BOPOH
  • @BOPOH really works, I tried a little more. Then the question is: what is $ , why does it match as a line break? It is clear that on the right side, \n becomes a line break. - Nick Volynkin ♦
  • ^ like $ is not a symbol, but a position in the text. The attachment cannot be replaced, but something can be inserted there. Those. replacing $ with something, you actually insert this “something” at the end - BOPOH

2 answers 2

Another option with sed:

tailf foo.log

 init.lxc 1455930731.917 ERROR lxc_initutils - initutils.c:mount_fs:36 - failed to mount /proc : Device or resource busy init.lxc 1455930758.674 ERROR lxc_initutils - initutils.c:mount_fs:36 - failed to mount /proc : Device or resource busy init.lxc 1455986152.381 ERROR lxc_initutils - initutils.c:mount_fs:36 - failed to mount /proc : Device or resource busy init.lxc 1455986234.349 ERROR lxc_initutils - initutils.c:mount_fs:36 - failed to mount /proc : Device or resource busy 

tailf foo.log | sed 'a\ '

 init.lxc 1455930731.917 ERROR lxc_initutils - initutils.c:mount_fs:36 - failed to mount /proc : Device or resource busy init.lxc 1455930758.674 ERROR lxc_initutils - initutils.c:mount_fs:36 - failed to mount /proc : Device or resource busy init.lxc 1455986152.381 ERROR lxc_initutils - initutils.c:mount_fs:36 - failed to mount /proc : Device or resource busy init.lxc 1455986234.349 ERROR lxc_initutils - initutils.c:mount_fs:36 - failed to mount /proc : Device or resource busy 
  • one
    Apparently a is add. - Nick Volynkin ♦
  • @NickVolynkin: yes, that's right. The a\ command adds a new line with the content that is specified after \, in this case an empty line with a space is simply added. - edem
  • one
    I will accept this option as more concise. ) - Nick Volynkin ♦
  • one
    Already 15 times used. I always have logs of 2-5 lines) - Nick Volynkin ♦

you need at the end of the line (and sed works exactly line by line ) to insert the newline character \n .

the anchor (metacharacter, quantifier), denoting the end of a line, in most dialects of regular expressions is usually the $ character:

 $ tail -f /var/log/some.log | sed 's/$/\n/' 

for isolation from the “intervention” of the shell, it is better to put the program for sed in single quotes (yes, s/$/\n/ is exactly the program interpreted by the sed program).