请帮我拿一下awk

文件1文件1

1
2
3

文件2文件2

23
45
27
67
867

输出

1:23
1:45
1:27
1:67
1:867
2:23
2:45
2:27
2:67
2:867
3:23
3:45
3:27
3:67
3:867

please help me how to command this 输出 using awk using delimeter ':' from 文件1文件1 and 文件2文件2 like 输出

推荐答案

有很多方法可以解决您的问题;这适合您的用例吗?

awk 'NR==FNR{a[++n]=$0; next} {for (i=1; i<=n; i++) {print $0 ":" a[i]}}' file2 file1
1:23
1:45
1:27
1:67
1:867
2:23
2:45
2:27
2:67
2:867
3:23
3:45
3:27
3:67
3:867

具有更好的格式和注释

awk '
NR==FNR{                                # for the first file
       a[++n]=$0                         # load values into an array
       next                             # then skip to the next line/file
}

{
       for (i=1; i<=n; i++) {   # for all the items in the array
              print $0 ":" a[i]         # print the value from file1, then the value from the array
       }
}' file2 file1
1:23
1:45
1:27
1:67
1:867
2:23
2:45
2:27
2:67
2:867
3:23
3:45
3:27
3:67
3:867

或者,也许使用GNU并行:

parallel echo {1}":"{2} :::: file1 :::: file2
1:23
1:45
1:27
1:67
1:867
2:23
2:45
2:27
2:67
2:867
3:23
3:45
3:27
3:67
3:867

编辑

如果您的输入中有<br/>个字符,则可以使用以下命令删除任何不是数字的内容:

awk 'NR==FNR{gsub("[^0-9]", "", $0); a[++n]=$0; next} {for (i=1; i<=n; i++) {gsub("[^0-9]", "", $0); print $0 ":" a[i]}}' file2 file1

Linux相关问答推荐

通过shell 脚本基于AM/PM对日期时间字段进行排序

如果在两种模式之间找到文本,则使用sed删除它们之间的所有行

计算与文件第一列对应的第二列中的字符串出现次数

/proc/mounts 没有像 /proc/self/mountinfo 这样的源信息

Perl/cpan: 找不到 JSON/Parse.pm @INC

如何在linux中将2个不同大小的图像(边框,实际图像)合并为1个

根据其他列的值创建一个新列

Dockerfile:无法复制文件

Docker 容器立即退出

如何重新安装最新的 cmake 版本?

为什么在 Linux 中使用 select

Linux C程序:如何找到函数所属的库

-zxvf 在 tar -zxvf <文件名> 中是什么意思?

如何将整个文件夹从 Amazon EC2 Linux 实例复制到本地 Linux 机器?

如何通过 ssh 判断 ubuntu 服务器上是否存在 php 和 apache

to_string不是std的成员?

如何设置errno值?

将 jiffies 转换为毫秒

命令行 Arduino 编译和上传?

PostgreSQL psql 终端命令