Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.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
Bash 如何提取具有公共字段的行_Bash_Sed_Awk_Uniq - Fatal编程技术网

Bash 如何提取具有公共字段的行

Bash 如何提取具有公共字段的行,bash,sed,awk,uniq,Bash,Sed,Awk,Uniq,我有一个这样的文件- 1 2 3 1 4 5 a z 3 a 3 4 a f g b b g 我想将其拆分为多个文件(尽可能多的组),每个文件都包含具有相同第一个字段的行 1 2 3 1 4 5 a z 3 a 3 4 a f g b b g 我该怎么做?我尝试了uniq--all repeated=separate-w32,但在查找重复项时,它考虑的是完整的行,而不仅仅是第一列。类似这样的内容: $ awk '{print > $1}' input $ cat 1

我有一个这样的文件-

1 2 3
1 4 5
a z 3
a 3 4
a f g
b b g
我想将其拆分为多个文件(尽可能多的组),每个文件都包含具有相同第一个字段的行

 1 2 3
 1 4 5

 a z 3
 a 3 4
 a f g

 b b g
我该怎么做?我尝试了
uniq--all repeated=separate-w32
,但在查找重复项时,它考虑的是完整的行,而不仅仅是第一列。

类似这样的内容:

$ awk '{print > $1}' input

$ cat 1
1 2 3
1 4 5

$ cat a
a z 3
a 3 4
a f g

$ cat b
b b g

稍微好一点的文件命名方法:

$ ls
file

$ awk '!($1 in a){a[$1]="file"++i}{print > a[$1]}' file

$ ls
file  file1  file2  file3

$ cat file1
1 2 3
1 4 5

$ cat file2
a z 3
a 3 4
a f g

$ cat file3
b b g

是否为您对它们进行
排序
分组?您希望
>
而不是
>
awks重定向操作符与shell不同。确实,filehandle在脚本期间是打开的。更新的
$1
是行上的第一个字段,
$2
是第二个字段,依此类推<代码>打印,打印指向以第一个字段命名的文件的整行。文件句柄在脚本期间是打开的,因此将向其追加行。