sed - “未知命令"用文件内容替换模式时 [英] sed - "unknown command" when replacing pattern with contents of file

查看:136
本文介绍了sed - “未知命令"用文件内容替换模式时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试操纵一些模板化"文本文件.这意味着文件包含一些令牌".需要由一些实际字符串替换 - 硬编码或从其他文件中读取.

I try to manipulate some "templated" text files. Meaning that the files contain some "tokens" which need to be replaced by some actual strings - either hard coded, or readable from other files.

我有以下两行:

    sed -r -e 's_<!-- cssintern -->_<style type="text/css">\n<!-- cssintern -->\n</style>\n_' -i "$out_dir_aca/books_1by1/independent/$(basename "$aca_frag").html"
    sed -e '^<!-- cssintern -->^' -e '{r '"$src_dir_frags_struct/styles1.css.frag" -e 'd}' -i "$out_dir_aca/books_1by1/independent/$(basename "$aca_frag").html"

第一个工作正常.问题是第二个生成了一个未知命令".错误,我想不出如何修复它.调试输出如下(最后一行的错误文本):

The first one works fine. The problem is that the second one generates an "unkown command" error, and I ran out of ideas how to fix it. The debugging output is the following (error text on last line):

+ for aca_frag in $out_dir_aca/frags_1by1/*
+ '[' -f '/home/vici/Dropbox/MyFiles/work/___rug/_!_workarea/2_txt_out/aca/frags_1by1/09.14 - Acatistul inaltarii Sfintei Cruci.frag' ']'
++ basename '/home/vici/Dropbox/MyFiles/work/___rug/_!_workarea/2_txt_out/aca/frags_1by1/09.14 - Acatistul inaltarii Sfintei Cruci.frag'
+ echo '    09.14 - Acatistul inaltarii Sfintei Cruci.frag'
    09.14 - Acatistul inaltarii Sfintei Cruci.frag
+ cat '/home/vici/Dropbox/MyFiles/work/___rug/_!_workarea/1_txt_in/##_frags_struct_##/doctypeXHTML.frag'
++ basename '/home/vici/Dropbox/MyFiles/work/___rug/_!_workarea/2_txt_out/aca/frags_1by1/09.14 - Acatistul inaltarii Sfintei Cruci.frag'
+ cat '/home/vici/Dropbox/MyFiles/work/___rug/_!_workarea/1_txt_in/##_frags_struct_##/head.frag'
++ basename '/home/vici/Dropbox/MyFiles/work/___rug/_!_workarea/2_txt_out/aca/frags_1by1/09.14 - Acatistul inaltarii Sfintei Cruci.frag'
+ cat '/home/vici/Dropbox/MyFiles/work/___rug/_!_workarea/1_txt_in/##_frags_struct_##/foot.frag'
++ basename '/home/vici/Dropbox/MyFiles/work/___rug/_!_workarea/2_txt_out/aca/frags_1by1/09.14 - Acatistul inaltarii Sfintei Cruci.frag'
++ basename '/home/vici/Dropbox/MyFiles/work/___rug/_!_workarea/2_txt_out/aca/frags_1by1/09.14 - Acatistul inaltarii Sfintei Cruci.frag'
+ sed -r -e 's_<!-- cssintern -->_<style type="text/css">\n<!-- cssintern -->\n</style>\n_' -i '/home/vici/Dropbox/MyFiles/work/___rug/_!_workarea/2_txt_out/aca/books_1by1/independent/09.14 - Acatistul inaltarii Sfintei Cruci.frag.html'
++ basename '/home/vici/Dropbox/MyFiles/work/___rug/_!_workarea/2_txt_out/aca/frags_1by1/09.14 - Acatistul inaltarii Sfintei Cruci.frag'
+ sed -re '^<!-- cssintern -->^' -re '{r /home/vici/Dropbox/MyFiles/work/___rug/_!_workarea/1_txt_in/##_frags_struct_##/styles1.css.frag' -re 'd}' -i '/home/vici/Dropbox/MyFiles/work/___rug/_!_workarea/2_txt_out/aca/books_1by1/independent/09.14 - Acatistul inaltarii Sfintei Cruci.frag.html'
sed: -e expression #1, char 1: unknown command: `^'

我用于分隔符^_/,我在all"中播放;带有简单和双引号的方法,以及我在互联网上找到的所有其他内容.错误仍然存​​在这一事实没有任何改变.

I used for separators ^, _, /, I played in "all" ways with simple and double quotation marks, and everything else that I found on the internet. Nothing changes the fact that the error remains.

过去我用过这条线

sed.exe -i -e '/%%H/{r ..\/##_frags_##\/out\/%%H' -e 'd}' .\out\out.frag

在 Windows cmd 脚本中,并且工作正常.(%%H 引用文件名,当用for 解析文件夹中的所有文件时).我实际上是在 Linux 中重写旧脚本,作为练习 - 希望增加简单性和额外功能.

in a Windows cmd script, and worked fine. (%%H references the name of the file, when parsing all the files in the folder with a for). I am actually rewriting the old scripts in Linux, as exercize - hopefully to add simplicity and extra features.

我打算修改以下 sed 命令:

I intend to adapt the following sed command:

sed -e '/<TEXT1>/{r File1' -e 'd}' -i File2

满足我的特殊需求.

注意:我提供了确切的文件名和路径,认为它们可能会提供一些见解.

Note: I provided the exact file names and paths, thinking that they might provide an insight.

注意:我在虚拟机中使用 Debian 10.7.

Note: I use Debian 10.7 in a virtual machine.

注意:脚本文件以#!/bin/bash -xv开头.-xv 显然只在调试时使用,否则会丢失.

Note: the script file starts with #!/bin/bash -xv. -xv is obviously used only when debugging, otherwise missing.

推荐答案

The语法是(我的评论):

[2addr] {editing command       # No newline between '[2addr]' and '{'!
editing command
...
}

其中 [2addr] 是例如 /filtering/ 表达式.选项 -e 确实(强调我的):

Where [2addr] is for example the /filtering/ expressions. The option -e does (emphasis mine):

如果指定了任何 -e 或 -f 选项,则编辑命令的脚本最初应为空.每个 -e 或 -f 选项指定的命令应按指定的顺序添加到脚本中.每次添加时,如果前一次添加(如果有)来自 -e 选项,则 <newline>应插入在新添加之前.生成的脚本应具有与操作数部分中描述的脚本操作数相同的属性.

If any -e or -f options are specified, the script of editing commands shall initially be empty. The commands specified by each -e or -f option shall be added to the script in the order specified. When each addition is made, if the previous addition (if any) was from a -e option, a <newline> shall be inserted before the new addition. The resulting script shall have the same properties as the script operand, described in the OPERANDS section.

[2addr]{ 之间有换行符时,脚本无效:

When there is a newline between [2addr] and { the script is invalid:

$ sed -e '/something/' -e '{' -e '}' /dev/null
sed: -e expression #1, char 11: missing command
$ sed '
/something/
{
}' /dev/null
sed: -e expression #1, char 13: unknown command: `
'

{[2addr] 一起放置可以解决问题,因为它删除了换行符:

Putting the { with [2addr] fixes the problem as it removes the newline:

$ sed -e '/somethimg/{' -e '}' /dev/null

所以把它和过滤表达式放在一起:

So put it with the filtering expression:

sed
   -e '\^<!-- cssintern -->^{'
   #   ^^                  ^^ here '{' is with /expression/
   # or just: -e '/<!-- cssintern -->/{'
   -e 'r blabla/styles1.css.frag'
   -e d
   -e '}' -i 'blabla/12.17 - Acatistul Sfantului Proroc Daniel.frag.html'

应该没问题.

这篇关于sed - “未知命令"用文件内容替换模式时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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