Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从两行向密钥对中追加值_Python_Perl_Bash - Fatal编程技术网

Python 从两行向密钥对中追加值

Python 从两行向密钥对中追加值,python,perl,bash,Python,Perl,Bash,我的文本文件输出在两行上如下所示: DelayTimeThreshold|MaxDelayPerMinute|Name 10000|5|rca 我想要的输出太小,如下所示: DelayTimeThreshold 10000 MaxDelayPerMinute 5 Name rca 我不想尝试这个: sed '$!N;s/|/\n/' foo 欢迎您提供任何建议,谢谢。因为您只有两行,这可以是一种方式: $ paste -d' ' <(head -1 file | sed 's/|/\

我的文本文件输出在两行上如下所示:

DelayTimeThreshold|MaxDelayPerMinute|Name
10000|5|rca
我想要的输出太小,如下所示:

DelayTimeThreshold 10000
MaxDelayPerMinute 5
Name rca
我不想尝试这个:

sed '$!N;s/|/\n/' foo

欢迎您提供任何建议,谢谢。

因为您只有两行,这可以是一种方式:

$ paste -d' ' <(head -1 file | sed 's/|/\n/g') <(tail -1 file | sed 's/|/\n/g')
DelayTimeThreshold 10000
MaxDelayPerMinute 5
Name rca
并对最后一行执行相同的操作:

$ tail -1 file | sed 's/|/\n/g'
10000
5
rca
然后,只需使用空格作为分隔符粘贴两个结果:

paste -d' ' output1 output2
作为perl脚本:

#!/usr/bin/perl -w
use strict; 
use Data::Dumper;

my $file1 = ('DelayTimeThreshold|MaxDelayPerMinute|Name');
my $file2 = ('10000|5|rca');

my @file1 = split('\|', $file1);
my @file2 = split('\|', $file2);

my %hash;

@hash{@file1} = @file2;

print Dumper \%hash;
输出:

$VAR1 = {
          'Name' => 'rca',
          'DelayTimeThreshold' => '10000',
          'MaxDelayPerMinute' => '5'
        };
DelayTimeThreshold 10000
MaxDelayPerMinute 5
Name rca
或:


此awk单衬里适用于您的要求:

awk -F'|' '!f{gsub(/\||$/," %s\n");f=$0;next}{printf f,$1,$2,$3}' file
输出:

kent$ echo "DelayTimeThreshold|MaxDelayPerMinute|Name
10000|5|rca"|awk -F'|' '!f{gsub(/\||$/," %s\n");f=$0;next}{printf f,$1,$2,$3}'  
DelayTimeThreshold 10000
MaxDelayPerMinute 5
Name rca
使用模块:


例如,假设您有一个文件,其中包含一个具有列名的标题行,后跟多个具有列值的详细信息行

DelayTimeThreshold|MaxDelayPerMinute|Name
10000|5|abc
20001|6|def
30002|7|ghk
40003|8|jkl
50004|9|mnp
下面的代码将使用第一行中的名称和后续(详细信息)行中的值打印该文件

#/bin/perl-w
严格使用;
my($fn,$fh)=(“header.csv”)#无论文件名是什么。。。
打开($fh,<$fn”)| |错误“无法打开$fn”;
我的($count,$line,@name,@vals)=(0);
while()
{
大口大口;
@VAL=拆分(/\ \124;/,$);

如果($count++您将其标记为python,但您说希望通过Bash或Perl获得解决方案-您想要什么?这是个好消息,@gordy!既然您是新来的,请不要忘记在您的问题已经解决的情况下将答案标记为已接受。您可以单击答案旁边的复选标记将其从空心切换为绿色。查看您是否有任何疑问bt!我喜欢你的
粘贴
解决方案。
+1
。你也可以不使用
/
,只需使用
sed
粘贴-d''谢谢,@gniourf:)我喜欢我的版本带有
head
tail
,我不熟悉
sed1s/
,但请更新您的评论,以便人们在寻找替代方案时可以轻松看到。
kent$ echo "DelayTimeThreshold|MaxDelayPerMinute|Name
10000|5|rca"|awk -F'|' '!f{gsub(/\||$/," %s\n");f=$0;next}{printf f,$1,$2,$3}'  
DelayTimeThreshold 10000
MaxDelayPerMinute 5
Name rca
perl -MArray::Transpose -F'\|' -lane '
    push @a, [@F]
    } END {print for map {join " ", @$_} transpose(\@a)
' <<END
DelayTimeThreshold|MaxDelayPerMinute|Name
10000|5|rca
END
DelayTimeThreshold 10000
MaxDelayPerMinute 5
Name rca
DelayTimeThreshold|MaxDelayPerMinute|Name
10000|5|abc
20001|6|def
30002|7|ghk
40003|8|jkl
50004|9|mnp
#!/bin/perl -w
use strict;
my ($fn,$fh)=("header.csv"); #whatever the file is named...
open($fh,"< $fn") || error "cannot open $fn";
my ($count,$line,@names,@vals)=(0);
while(<$fh>)
{
    chomp $_;
    @vals=split(/\|/,$_);
    if($count++<1) { @names=@vals; next; } #first line is names
    for (my $ndx=0; $ndx<=$#names; $ndx++) { #print each
        print "$names[$ndx] $vals[$ndx]\n";
    }
}
my %row;
my @records;
while(<$fh>)
{
    chomp $_;
    @vals=split(/\|/,$_);
    if($count++<1) { @names=@vals; next; }
    @row{@names} = @vals;
    push(@records,\%row);
}
my %row;
my %records;
while(<$fh>)
{
    chomp $_;
    @vals=split(/\|/,$_);
    if($count++<1) { @names=@vals; next; }
    @row{@names} = @vals;
    $records{$vals[0]}=\%row;
}