2个文本文件中的数据 - 无法将变量写入输出文件

时间:2016-02-10 16:03:48

标签: powershell variables

我的帖子以前被标记为重复,但我指出的比较对象线程并没有帮助我解决我遇到的问题。有谁知道我如何解决这个问题?感谢

我写了一个小脚本来比较'file 1'中的文本和'file 2'中的文字。

如果“文件1”中的文本存在于“文件2”中,则屏幕上会显示“条目存在”。

如果“文件1”中的文本不存在于“文件2”中,则“不存在”会附加到“文件2”。

这是有效的,但我真正希望能做的不是在'文件2'中附加'不存在',我想追加'文件2中不存在的值'到'文件2'的结尾。每当我尝试这个时,我都会在“文件2”中附加一个空行。

这是脚本:

$InputFile = "d:\temp\Input.txt"
$Input = Get-Content $InputFile
$AppendFile = "d:\temp\appendfile.txt"
foreach ($line in $Input)  {
                if ($line = Select-String -AllMatches ^$line$ -path $AppendFile) {
                                Write-Host "Entry exists"}
                               else {
                                Write-output "$line" | out-file -width 300 -append $AppendFile
                                     }
                }

1 个答案:

答案 0 :(得分:0)

这是更新的解决方案(使用的常用别名):

write "A`nB" | sc append.txt
write "A`nB`nC" | sc input.txt
$append = (gc append.txt); 
(gc input.txt) | 
 %{ 
   $inputLine = $_; 
   if(-not ($append | ?{ $_ -like  $inputLine })) { 
     Add-Content -Path append.txt -Value $inputLine 
   } 
  }; 
gc append.txt # displays 'A`nB`nC'