1

I'd like to know if there's any way of altering output of every command I execute in terminal. The purpose is to make the output formated for my markdown howto's.

E.g.

ls -la .*z
#>-rw-r--r--  1 hologos  staff   3580 26 zář 14:22 .zconfig
#>-rw-------  1 hologos  staff  28927 17 říj 15:59 .zhistory
#>-rw-r--r--  1 hologos  staff   5600 30 zář 08:03 .zshrc

The "#>" is what I want every line of output starts with this. I primarely use zsh.

Thank you.

2 Answers 2

2

I'd do something like

  command | sed -e 's/^/line-prefix /'

example

$ ls -la m* | sed -e 's/^/#> /'
#> -rwxr-xr-x 1 root root 76080 Aug 11  2011 mailstats
#> -r-xr-xr-x 1 root root 92824 Aug 11  2011 makemap
#> -rwxr-xr-- 1 root root 12389 Jul 22  2011 makewhatis
#> -rwxr-xr-x 1 root root 11520 Mar  6  2011 matchpathcon
#> -rwxr-xr-x 1 root root 56600 May 18  2011 mcelog
#> lrwxrwxrwx 1 root root    15 Mar 18  2011 mkdict -> cracklib-format
#> -rwxr-xr-x 1 root root  9600 Jul 22  2011 mklost+found
#> -rwxr-xr-x 1 root root 10176 Jan  6  2007 mksock
#> -rwxr-xr-x 1 root root  4409 Jan  7  2007 modeline2fb
#> -rwxr-xr-x 1 root root 23787 Jul 22  2011 mountstats
#> -rwxr-xr-x 1 root root 58032 Jan  7  2007 mtr

You can also apply this to a shell so it is done automatically

$ ksh | sed -e 's/^/#> /'
$ ls m*
#> mailstats
#> makemap
#> makewhatis
#> matchpathcon
#> mcelog
#> mkdict
#> mklost+found
#> mksock
#> modeline2fb
#> mountstats
#> mtr

There's probably some drawbacks/gotchas, take care with this.


A different approach is to run script (see man script) and then use your favourite editor (or a small perl/awk/etc script) to make it markdown-friendly - In vim I'd just do something like :%s/^/#> / to fix all the lines in one go. Then I'd insert markdown headers etc.

2
  • This is exactly what I need. I'm planning on using it while executing all kinds of commands. Are there any drawbacks/gotchas you can think of?
    – Hologos
    Oct 17, 2014 at 14:37
  • @Hologos: Some commands behave differently if STDOUT is piped to a process rather than connected to a TTY. Also there's some wierdness with the shell prompting that doesn't seem to cause any harm but which is disconcerting. There's probably more. Oct 17, 2014 at 14:45
1
IFS=$'\n';for line in `ls -la .*z`;do echo "#>$line";done

it will loop through command output (ls -la .*z) and display the result on the screen by prepending #> to every line

ps:

the IFS part is for setting the delimiter to new line instead of whitespace

3
  • Thank you, is there any way I shouldn't have to write this? Something like cd() { builtin cd "$@" && ll; } but for every command or some type of alias which would alter every command in the background?
    – Hologos
    Oct 17, 2014 at 14:21
  • why would you avoid using the for loop?
    – user373230
    Oct 17, 2014 at 14:29
  • Or something like ls -la .z* but it would automaticaly expand it as ls -la .z* | my-program, which would contain your code.
    – Hologos
    Oct 17, 2014 at 14:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .