File SED用文件中的变量行替换每个占位符
Var4:File SED用文件中的变量行替换每个占位符,file,variables,text,sed,replace,File,Variables,Text,Sed,Replace,Var4: 要替换的占位符 replace.txt 包含变量数据 input.txt: 输入文件 output.txt 输出文件 “input.txt”中出现的每个“Var4”都应替换为“replace.txt”中的一个变量行,但sed将“replace.txt”作为普通文本而不是文件名读取: 不正确: TextBox1.Value=“r replace.txt” 正确: TextBox1.Value=“0000AAAA” 我厌倦了搜索所有类似的帖子,寻找正确的语法,但没有用 多谢各位 Inpu
要替换的占位符 replace.txt
包含变量数据 input.txt:
输入文件 output.txt
输出文件 “input.txt”中出现的每个“Var4”都应替换为“replace.txt”中的一个变量行,但sed将“replace.txt”作为普通文本而不是文件名读取: 不正确: TextBox1.Value=“r replace.txt” 正确: TextBox1.Value=“0000AAAA” 我厌倦了搜索所有类似的帖子,寻找正确的语法,但没有用 多谢各位 Input.txt
sed -e "s/Var4/r {replace.txt} /g" "input.txt" > "output.txt"
sed '/Var4/R replace.txt' input.txt | sed '/Var4/{N;s/"Var4"\n\(.*\)/"\1"/}' > "output.txt"
Output.txt应如下所示:
TextBox1.Value = "Var4"
Else
TextBox1.Value = "- - - - - - -"
End If
TextBox1.Value = "Var4"
Else
TextBox1.Value = "- - - - - - -"
End If
TextBox1.Value = "Var4" Then
Else
TextBox1.Value = "- - - - - - -"
End If
TextBox1.Value = "Var4" Then
Else
TextBox1.Value = "- - - - - - -"
End If
Replace.txt
TextBox1.Value = "0000AAAA"
Else
TextBox1.Value = "- - - - - - -"
End If
TextBox1.Value = "0000BBBB"
Else
TextBox1.Value = "- - - - - - -"
End If
TextBox1.Value = "0000AAAA" Then
Else
TextBox1.Value = "- - - - - - -"
End If
TextBox1.Value = "0000BBBB" Then
Else
TextBox1.Value = "- - - - - - -"
End If
我也试过了
0000AAAA
0000BBBB
0000CCCC
更新1:
TextBox1.Value = "0000AAAA
0000BBBB
0000CCCC
"
Else
TextBox1.Value = "- - - - - - -"
End If
TextBox1.Value = "0000AAAA
0000BBBB
0000CCCC
"
Else
TextBox1.Value = "- - - - - - -"
End If
如果“Var4”位于行的末尾,则可以正常工作,但是如果其后存在任何文本,则输出不正确
Input.txt
sed -e "s/Var4/r {replace.txt} /g" "input.txt" > "output.txt"
sed '/Var4/R replace.txt' input.txt | sed '/Var4/{N;s/"Var4"\n\(.*\)/"\1"/}' > "output.txt"
Output.txt应如下所示:
TextBox1.Value = "Var4"
Else
TextBox1.Value = "- - - - - - -"
End If
TextBox1.Value = "Var4"
Else
TextBox1.Value = "- - - - - - -"
End If
TextBox1.Value = "Var4" Then
Else
TextBox1.Value = "- - - - - - -"
End If
TextBox1.Value = "Var4" Then
Else
TextBox1.Value = "- - - - - - -"
End If
Replace.txt
TextBox1.Value = "0000AAAA"
Else
TextBox1.Value = "- - - - - - -"
End If
TextBox1.Value = "0000BBBB"
Else
TextBox1.Value = "- - - - - - -"
End If
TextBox1.Value = "0000AAAA" Then
Else
TextBox1.Value = "- - - - - - -"
End If
TextBox1.Value = "0000BBBB" Then
Else
TextBox1.Value = "- - - - - - -"
End If
使用后的Output.txt:
0000AAAA
0000BBBB
0000CCCC
有什么建议吗?
sed
的r
命令只能将文件内容输出到stdout。您不能在模式空间中获取该内容,也不能将该内容注入另一个sed
命令(如您问题中的命令)
只需使用shell即可实现这一点。以下是一个使用Bourne shell的解决方案,因为我不了解Windows:
sed '/Var4/R replace.txt' input.txt | sed '/Var4/{N;s/"Var4"\n\(.*\)/"\1"/}' > "output.txt"
TextBox1.Value = "Var4" Then
0000AAAA
Else
TextBox1.Value = "- - - - - - -"
End If
TextBox1.Value = "Var4" Then
0000BBBB
Else
TextBox1.Value = "- - - - - - -"
End If
这里我假设replace.txt
的内容与您的示例(0000AAAA)类似,并且不包含任何“/”,否则在s///
命令中选择另一个“/”以外的分隔符。这可能适用于您(GNU-sed):
在占位符之后追加替换行,然后在单独的sed调用中,在占位符之后追加该行,并使用substitution命令实现所需的结果
注意:R
命令一次只读取一行以下文件,而R
命令每次调用时都读取整个文件
sed '/Var4/R replaceFile' file | sed '/Var4/{N;s/"Var4"\(.*\)\n\(.*\)/"\2"\1/}'
谢谢>这在GNUBash上有效 一,。在windows cmd>下,输出仍然不正确的TextBox1.Value=“r replace.txt”2。在bash>error>unterminated's'命令1下。毫不奇怪,我没有对您的Windows命令进行任何增强。2.您的文件
replace.txt
可能包含多行。它应该包含一行0000AAAA(或任何其他数据,只要里面没有“/”)。如果要在output.txt中插入行跳过,请使用\n
分隔行,如:0000AAAA\n0000BBBB\n0000CCCC
如果要在标有Var4
的位置插入replace.txt
的全部内容,请参阅我之前的注释。如果你想在这3000行中插入一行,那么我不理解你的问题:在你的问题中解释该行应该如何选择,并给出一个经过仔细选择的replace.txt
和input.txt
的小样本。为了保持一致性,我在windows上安装了bash,它可以工作,这只是获得正确语法以获得正确结果的问题。请参阅我发布的示例。除了Windows 10中的WSL选项外,Windows本机不包括sed
,因此您必须从其他来源获得它;大多数这样的源代码还提供或提供awk
和/或perl
,这两种源代码都更适合解决这个问题。我在windows上安装了bash以保持一致性,它可以正常工作,只需使用正确的语法即可获得正确的结果。干得好!我刚刚更改了输入和输出文件名以匹配原始的post.sed'/Var4/R replace.txt'input.txt | sed'/Var4/{N;s/“Var4”\N(.*)/“\1”/}'>“output.txt”可以工作,但“Var4”必须在行的末尾!如果后面有文字怎么办?@potong Perfect:-)如果你想感谢potong,只需在他们的回答中添加一条评论,然后单击旁边的向上箭头。