Linux 如何使用shell脚本将两列分隔成数组
这是我当前使用的示例数据集:Linux 如何使用shell脚本将两列分隔成数组,linux,bash,perl,shell,Linux,Bash,Perl,Shell,这是我当前使用的示例数据集: ID Name command xxx xxxxx xxxxx xxxx xxxxx 5474558 abcd run xxx xxxxx xxxxx xxxx xxxxx 8487848 xyze bash xxx xxxxx xxxxx xxxx xxxxx ID和名称之间的距离为8个空格 我需要在两个单独的数组中输入ID和名称
ID Name command xxx xxxxx xxxxx xxxx xxxxx
5474558 abcd run xxx xxxxx xxxxx xxxx xxxxx
8487848 xyze bash xxx xxxxx xxxxx xxxx xxxxx
ID和名称之间的距离为8个空格
我需要在两个单独的数组中输入ID和名称
我需要的样本输出是:
id=[5474558,8487848]
name=[abcd,xyze]
使用shell脚本如何执行此操作?perl中的解决方案:
use warnings;
use strict;
open my $fh, '<', "file.txt" or die "couldn't open file : $!";
while( <$fh> ) {
next if $. == 1;
chomp;
my @columns = split /\s+/, $_;
push (my @ids, $columns[0]);
push (my @names, $columns[1]);
}
使用警告;
严格使用;
打开我的$fh,“试试这个命令,它会打印ID并命名这只是一个提示
身份证
awk '{if (NR !=1 ) printf $1}' data.txt
为了名字
awk '{if (NR !=1 ) printf $2}' data.txt
请发布您尝试过的内容以及您看到的错误。您添加了预期输出,但没有显示您尝试过的脚本?为什么人们投票反对???