拆分标准输出并管道每个命令

时间:2015-08-26 17:56:31

标签: javascript node.js bash unix pipe

我有一个<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> <section name="nlog" type="NLog.Config.ConfigSectionHandler, NLog" /> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> </configSections> <connectionStrings> <add name="StrantinEDMEntities" connectionString="metadata=res://*/SEDM.csdl|res://*/SEDM.ssdl|res://*/SEDM.msl;provider=System.Data.SqlServerCe.4.0;provider connection string=&quot;Data Source=D:\SDB001.sdf&quot;" providerName="System.Data.EntityClient" /> </connectionStrings> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" /> </startup> <runtime> <dependentAssembly> <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-2.2.22.0" newVersion="2.2.22.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Net.Http.Primitives" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-2.2.22.0" newVersion="2.2.22.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="log4net" publicKeyToken="669e0ddf0bb1aa2a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-1.2.13.0" newVersion="1.2.13.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Microsoft.Threading.Tasks.Extensions.Desktop" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-1.0.168.0" newVersion="1.0.168.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> </dependentAssembly> </assemblyBinding> <loadFromRemoteSources enabled="true" /> </runtime> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework"> <parameters> <parameter value="System.Data.SqlServerCe.4.0" /> </parameters> </defaultConnectionFactory> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> <provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" /> </providers> </entityFramework> <system.data> <DbProviderFactories> <remove invariant="System.Data.SqlServerCe.4.0" /> <add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.1, Culture=neutral, PublicKeyToken=89845dcd8080cc91" /> </DbProviderFactories> </system.data> </configuration> 文件,看起来像这样。

javascript

我可以用两种方式运行。

var val = "foo"
console.log(val)
//EVALMD-STDOUT-FILE-DELIMETER
if (!val) var val = "bar"
console.log(val)
//EVALMD-STDOUT-FILE-DELIMETER
console.log(val)

或者我可以使用node example.js 将其封存。

cat

我试图找出如何在我的案例cat example.js | node 中用分隔符分割像cat example.js这样的标准输出,并将每个标记用//EVALMD-STDOUT-FILE-DELIMETER作为一个单独的进程管道,所以我没有记录nodefoofoo,而应该foofoo,然后是错误。

上面的文件应该拆分并解释为3&#39;

之一:

bar

2:

var val = "foo"
console.log(val)

3:

if (!val) var val = "bar"
console.log(val)

1 个答案:

答案 0 :(得分:0)

如果您事先知道拆分的数量,可以扩展的部分解决方案类似于:

cat example.js | tee >(sed -n '1,_//EVALMD-STDOUT-FILE-DELIMETER_p' | node) >(sed -n '_//EVALMD-STDOUT-FILE-DELIMETER_,$p' | node) >/dev/null

使用最后>/dev/null来保持tee不会将其打印到stdout

如果您事先不知道事情,可以使用perl来执行此操作:

open(my $out, "| node");
while(<>) {
    if(/\/\/EVALMD-STDOUT-FILE-DELIMETER/) {
        close($out);
        open($out, "| node");
        next;
    }
    print $out $_;
}

所以你可以把一个文件放到那里,你可以把它作为perl脚本,或者你可以折叠成一个字符串并像

那样运行它
cat example.js | perl -nle 'open(my $out, "| node");while(<>) {if(/\/\/EVALMD-STDOUT-FILE-DELIMETER/) {close($out);open($out, "| node");next;}print $out $_;}'

当然你可以简单地修改perl来读取文件本身,所以不需要cat