awk
: if first character is "l" (link), print fields 9,10,11; else only 9.
[ranga@garuda ~]$ ls -l $(which -a firefox)|awk '{print $1 ~ /^l/ ? $9 $10 $11 : $9 }'
/usr/bin/firefox->/usr/lib64/firefox/firefox
$1
is the first field
$1 ~ /^l/ ?
tests whether the first field matches the pattern ^l
(first character is "l")
- if the test passes,
print
receives $9 $10 $11
; else, only $9
.
sed
: remove first 8 non-space and space character bunches.
[ranga@garuda ~]$ ls -l $(which firefox) | sed 's/^\([^ ]*[ ]*\)\{8\}//'
/usr/bin/firefox -> /usr/lib/firefox/firefox
[ ]*
matches a bunch of contiguous spaces
[^ ]*
matches a contiguous bunch of non-space characters
- the grouping
\([^ ]*[ ]*\)
matches a text with one non-space bunch and one space bunch (in that order).
\{8\}
matches 8 contiguous instances of this combination. ^
at the beginning pins the match to the beginning of the line.
's/^\([^ ]*[ ]*\)\{8\}//'
replaces a match with empty text - effectively removing it.
seems to work so long as you aren't running "which" on an alias.
these commands are not presented inside a function but can be used in one (which you already know how to).