2

I'm using this command, which searches pacman.log for packages updated today and converts them into a conky string:

tail -500 /var/log/pacman.log | grep -e "\[$(date +"%Y-%m-%d") [0-5][0-9]:[0-9][0-9]\] \[ALPM\] upgraded" | sed 's/^.*\([0-2][0-9]:[0-5][0-9]\).*upgraded \([^ ]*\).*/${color2}\2${goto 250}${color1}\1/' | tail -18

With tail -18 the maximum number of lines is 18.

What is the best way to append new lines so that the stream always has 18 lines?

  • 1
    You’ve ... already limited it to 18 lines. Do you need to pad the output up to 18? – Jeff Schaller Oct 27 '17 at 9:36
  • It should always have excactly 18 lines because of padding. Now only the maximum lines is 18. – Rua4da Oct 27 '17 at 9:38
2

You can do (with a shell with support for zsh's {x..y} form of brace expansion like zsh, bash, ksh93 or yash -o braceexpand):

{
  printf '%.0s\n' {1..18}
  your-command
} | tail -n 18

Note that it prepends newline as opposed to appending them. To append, you could do:

your-command | tail -n 18 | awk '{print};END{while (NR++ < 18) print ""}'
  • For me it just outputs empty lines If I do it without awk output is ´${color2}apr${goto 250}${color1}02:51 ${color2}apr-util${goto 250}${color1}02:51 ${color2}chromium${goto 250}${color1}08:47´ – Rua4da Oct 27 '17 at 10:08
  • @Rua4da, d'uh sorry, I forgot the {print}, I keep doing it. See edit. – Stéphane Chazelas Oct 27 '17 at 10:10
0

With bash you can use c-style loop:

other_command | for((i=0;i<18;i++)); do IFS= read -r line; printf '%s\n' "$line";done

But where you don't have bash, use awk Stéphane Chazelas suggests.

  • 1
    Strictly speaking, that's ksh93 syntax (now also recognised by zsh and bash). It still needs the tail -n 18 as otherwise it would get the head instead of tail. – Stéphane Chazelas Oct 27 '17 at 10:02

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.