在一行的最后一个单词前添加单词 [英] pre-pend word to the last word of a line

查看:44
本文介绍了在一行的最后一个单词前添加单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一行中的最后一个单词前添加一个目录名.该行具有以下格式:

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$

其中 ^I 表示 tab$ 表示行尾.此行由 git ls-files -s 生成.

where ^I denotes a tab, and $ denotes the end-of-line. This line is generated by git ls-files -s.

我想要一个 sed 命令将 one/ 添加到此行中的文件名,如下所示:

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:

  1. 匹配不是\t后跟$的最长字符串;追加 one/:

  1. Match the longest string of characters that are not \t followed by $; append one/:

$ git ls-files -s | sed 's|[^\t]*$|one/&|'  
one/100644 bfadfab6f98b8fa1e9989fe16b2bf0fb13ffd39e 0   oneFile

  • 匹配不是\t' '后跟$的最长字符串;前置 one/:

  • Match the longest string of characters that are not \t or ' ' followed by $; pre-pend one/:

    $ git ls-files -s | sed 's|[^\t ]*$|one/&|'
    100644 bfadfab6f98b8fa1e9989fe16b2bf0fb13ffd39e one/0   oneFile
    

  • 匹配非水平空白的最长字符串,在前面加上'one/':

  • 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/&

    以及上面列出的那些.我能得到的最接近的是 oneFilone/e,即 [^[[:blank:]]]*$|one/&|',或者前置到 0,但我似乎无法完全得到我想要的.

    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.

    编辑因为有几个人评论/发布了答案,但没有一个对我有用,我想我会补充:我使用的是 Mac OS X 10.7.3.sed 的版本我不完全确定(如果有人知道一种方法可以随意添加评论) - man sed 页面说这是一个 BSD sed.我不确定这与 GNU sed 有什么不同,如果有的话.

    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.

    我也在使用 zsh,其中 oh-my-zsh 正在运行(几乎没有修改).我已打开 extended_glob (setopt extended_glob).

    I'm also using zsh, with oh-my-zsh running (prettymuch unmodified). I have turned on extended_glob (setopt extended_glob).

    对于人们给出的答案,我已经评论了我的结果;我假设它们在 Linux 发行版上运行?我今晚无法访问非 OS X 系统,但明天我将重新运行任何答案;也许只是我的 [shell|OS|bad karma] 没有让它们为我工作.

    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.

    再次

    所以我在 Ubuntu 系统上进行了测试,上面的 (1) 确实有效.不过,我希望有一个适用于我的 Mac 的工作版本.

    So I've tested on a Ubuntu system, and the (1) above does work. I'd love a working version for my Mac, though.

    最终感谢所有回答工作等效命令的人.事实证明,我的第一个确实可以工作,但不能使用 BSD sed.然而,我确实有 gsed 可用(感谢您指出这一点!)这使得这些都神奇地工作.

    Final 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.

    推荐答案

    这更简单,但似乎有效:

    This is simpler, but seems to work:

    echo -e '100644 bfadfab6f98b8fa1e9989fe16b2bf0fb13ffd39e 0 oneFile' | sed -e 's/\([a-zA-Z]\+\)$/one\/\0/g'
    100644 bfadfab6f98b8fa1e9989fe16b2bf0fb13ffd39e 0 one/oneFile
    

    它也应该与 oneFile 之前的选项卡一起使用.

    It should work with a tab just before oneFile also.

    这篇关于在一行的最后一个单词前添加单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆