在Win XP cmd解释器下的gnu sed脚本中的行继续使用

时间:2015-01-18 20:52:43

标签: windows batch-file sed continuation

(GNU sed版本4.0.7 - 为Win32编译 - 来自http://unxutils.sourceforge.net

要在大型txt文件的前面添加一行,以下单行批处理脚本可以正常工作:

gsed -i "1i longheader1  longheader2  longheader3 longheader4 ..." testfile.txt

但是,为了清楚起见,将文本字符串分割为多行来格式化批处理脚本会很有用,可能是这样的:

gsed -i "1i ^
 longheader1 ^
 longheader2 ^
 longheader3 ^
 longheader4" ^
 testfile.txt

不幸的是,执行上面的批处理脚本失败了:

  

'longheader1'无法识别为内部或外部命令,   可操作程序或批处理文件。

^替换行继续符\也会失败。

有关为什么“行继续”脚本失败以及潜在的简明解决方法的任何建议?

__菲利普

1 个答案:

答案 0 :(得分:0)

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
COPY /y c:threadfile.txt u:\testfile.txt >NUL
SET "sedinstruction="
FOR %%t IN (
1i 
longheader1
longheader2
longheader3 
longheader4 
...
) DO SET "sedinstruction=!sedinstruction! %%t"

sed -i "%sedinstruction%" u:\testfile.txt
TYPE u:\testf*

COPY /y c:threadfile.txt u:\testfile.txt >NUL
SET "sedinstruction="
FOR %%t IN (
"1i"
"longheader1"
" longheader2"
" longheader3 "
"longheader4"
"..."
) DO SET "sedinstruction=!sedinstruction! %%~t"

sed -i "%sedinstruction%" u:\testfile.txt
TYPE u:\testf*

GOTO :EOF

这是一种方式 - 也是主题的变体。

请注意,在原始说明中,有时会有1个,有时还有两个空格。第一个是arbirarily插入一个,第二个是引号中包含的数字。可能存在变化 - 如果"字符串"则引号是而不是。不包括空格。

请注意,这会对某些字符表现出一定的敏感度 - 尤其是%^