代码之家  ›  专栏  ›  技术社区  ›  DAG

bash使用sed在文件的第一个位置插入单词

sed
  •  0
  • DAG  · 技术社区  · 3 年前

    如何使用 sed 在文件的第一行起始位置插入单词。 例如,我的文件包含以下内容:

    that is a test without scope
    that is the second line
    that is the third line
    

    结果应该是:

    [TICKET_NUMBER] that is a test without scope
    that is the second line
    that is the third line
    

    我正在尝试此操作,但出现以下错误:

    sed -i -E "1s/^[$ISSUE_ID]/ " $COMMIT_EDITMSG_FILE
    

    sed:-e表达式#1,char 17:未终止的“s”命令

    我已经学习了一些例子,但我没有取得任何进展。

    1 回复  |  直到 3 年前
        1
  •  1
  •   anubhava    3 年前

    你可以用这个 sed :

    sed "1s/^/$ISSUE_ID /" file
    
    [TICKET_NUMBER] that is a test without scope
    that is the second line
    that is the third line
    

    此处:

    • 1s 将应用 s 仅第一行上的命令
    • ^ 将匹配行开始
    • "..." 将让shell变量展开