有没有办法使用bash shell逐行完成两个文件的组合[复制]
发布时间:2020-12-15 20:54:50 所属栏目:安全 来源:网络整理
导读:参见英文答案 Cartesian product of two files (as sets of lines) in GNU/Linux????????????????????????????????????11个 请看例子: 假设我的File1.txt有三行(a,b,c);文件2也有3行(1,2,3). 文件1 abc 档案2 123... 我想获得如下的A File3: 档案3 a1a2a3b
参见英文答案 >
Cartesian product of two files (as sets of lines) in GNU/Linux????????????????????????????????????11个
请看例子: 假设我的File1.txt有三行(a,b,c);文件2也有3行(1,2,3). 文件1 a b c 档案2 1 2 3 ... 我想获得如下的A File3: 档案3 a1 a2 a3 b1 b2 b3 c1 c2 c3 ... 非常感谢! 解决方法
假设bash 4.x:
#!/usr/bin/env bash # ^^^^-- NOT /bin/sh readarray -t a <file1 # read each line of file1 into an element of the array "a" readarray -t b <file2 # read each line of file2 into an element of the array "b" for itemA in "${a[@]}"; do for itemB in "${b[@]}"; do printf '%s%sn' "$itemA" "$itemB" done done 在旧的(4.0之前版本)bash版本中,您可以将readarray -t a< file1和readarray -t b< file2替换为: IFS=$'n' read -r -d '' -a a < <(cat -- file1 && printf ' ') IFS=$'n' read -r -d '' -a b < <(cat -- file2 && printf ' ') (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |