I want to pre-pend a directory name to the last word in a line. The line has the following format:
100644 bfadfab6f98b8fa1e9989fe16b2bf0fb13ffd39e 0^IoneFile$
where ^I
denotes a tab
, and $
denotes the end-of-line. This line is generated by git ls-files -s
.
I want a sed
command to prepend one/
to the filename in this line, like so:
`100644 bfadfab6f98b8fa1e9989fe16b2bf0fb13ffd39e 0^Ione/oneFile$
Some of the lines that I've tried, and their corresponding outputs:
Match the longest string of characters that are not
\t
followed by$
; appendone/
:$ git ls-files -s | sed 's|[^\t]*$|one/&|' one/100644 bfadfab6f98b8fa1e9989fe16b2bf0fb13ffd39e 0 oneFile
Match the longest string of characters that are not
\t
or' '
followed by$
; pre-pendone/
:$ git ls-files -s | sed 's|[^\t ]*$|one/&|' 100644 bfadfab6f98b8fa1e9989fe16b2bf0fb13ffd39e one/0 oneFile
Match the longest string of characters that are not horizontal whitespace, prepend 'one/':
$ git ls-files -s | sed 's|[^[[:blank:]]]*$|one/&|' 100644 bfadfab6f98b8fa1e9989fe16b2bf0fb13ffd39e 0 oneFilone/e
I've basically tried a whole bunch of things matching for:
[^\t ]*$|one/&
[^[[:space:]]]*$|one/&
and the ones listed above. The closest I can get is to have oneFilone/e
, which was [^[[:blank:]]]*$|one/&|'
, or to pre-pend to the 0
, but I can't seem to quite get what I want.
EDIT
Because a few people have commented / posted answers, none of which work for me, I figured I'd add: I am using Mac OS X 10.7.3. The version of sed
I'm not completely sure of (if anybody knows a way to get it feel free to add a comment to that effect) - the man sed
page says it's a BSD sed. I'm not sure how different that is to GNU sed
, if any.
I'm also using zsh
, with oh-my-zsh
running (prettymuch unmodified). I have turned on extended_glob
(setopt extended_glob
).
I've commented with my results for the answers people have given; I assume they are run on a Linux distribution? I don't have access to a non-OS X system tonight, but I will re-run any answers tomorrow; maybe it's just my [shell|OS|bad karma]
that isn't letting them work for me.
EDIT Again:
So I've tested on a Ubuntu system, and the (1) above does work. I'd love a working version for my Mac, though.
Final EDIT:
Thanks to all who answered with working equivalent commands. It turns out that my first one does work, but not with BSD sed
. I do, however, have gsed
available (thanks for pointing that out!) which makes these all magically work.