Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.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/7/google-maps/4.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 如何使用shell每隔n列插入一个分隔符_Bash_Shell_Awk - Fatal编程技术网

Bash 如何使用shell每隔n列插入一个分隔符

Bash 如何使用shell每隔n列插入一个分隔符,bash,shell,awk,Bash,Shell,Awk,我有如下数据 11111 22222 33333 44444 55555 66666 11111,22222,3333,4444 ,55555 ,66666 我希望输出数据如下 11111 22222 33333 44444 55555 66666 11111,22222,3333,4444

我有如下数据

11111 22222 33333 44444                       55555           66666
11111,22222,3333,4444                       ,55555           ,66666
我希望输出数据如下

11111 22222 33333 44444                       55555           66666
11111,22222,3333,4444                       ,55555           ,66666

我尝试使用tr命令,用逗号转换空格,但得到4444或55555后面的所有逗号。注意:4444和5555之间有很多空格。可以用逗号填充第6、12、17、46等列的空格吗

这不正是你想要的吗?(猜测所需输出中缺少的4是一个输入错误)


(如果您在mac电脑上,请使用
-r
而不是
-E

这不正是您想要的吗?(猜测所需输出中缺少的4是一个输入错误)


(如果您在mac电脑上,请使用
-r
而不是
-E

如果您的实际输入文件与所示示例相同,则以下内容也可能对您有所帮助

awk '{gsub(/ [0-9]+/,",&");gsub(/, /,",")} 1'   Input_file
输出如下

11111,22222,33333,44444                      ,55555          ,66666
对上述命令的解释:

awk '{
gsub(/ [0-9]+/,",&"); ##gsub is awk utility to substitute things, so its format is gsub(regex_for_strings_to_be_replaced,with_whom_it_should_be_replaced,variable/line). So here I am substituting globally space then all digits(continuous ones) with a comma and (matched regex itself, which is space then all digits(continuous ones)).
gsub(/, /,",")        ##Using gsub again here to replace comma then space(, ) with only comma(,), to get the output into same shape in which OP has asked, it will remove space between command and digits which was created in above command.
}
1                     ##awk works on method of condition then action, so here I am making condition as TRUE by mentioning 1 and not mentioning any action so be default print action will happen, print of current line.
' Input_file          ##Mentioning Input_file name here.

若您的实际输入文件与示例中所示的相同,则以下内容也可能对您有所帮助

awk '{gsub(/ [0-9]+/,",&");gsub(/, /,",")} 1'   Input_file
输出如下

11111,22222,33333,44444                      ,55555          ,66666
对上述命令的解释:

awk '{
gsub(/ [0-9]+/,",&"); ##gsub is awk utility to substitute things, so its format is gsub(regex_for_strings_to_be_replaced,with_whom_it_should_be_replaced,variable/line). So here I am substituting globally space then all digits(continuous ones) with a comma and (matched regex itself, which is space then all digits(continuous ones)).
gsub(/, /,",")        ##Using gsub again here to replace comma then space(, ) with only comma(,), to get the output into same shape in which OP has asked, it will remove space between command and digits which was created in above command.
}
1                     ##awk works on method of condition then action, so here I am making condition as TRUE by mentioning 1 and not mentioning any action so be default print action will happen, print of current line.
' Input_file          ##Mentioning Input_file name here.

在这里大声想一想,但试着用逗号替换空格,然后用“`(空格)替换
,你真的想在输出中少一个
3
和一个
4
?谢谢你的快速回复。我能够解决这个问题。因为我必须插入逗号的字段是常量,所以我使用了multiplesed命令。因此,对于上面的示例,cmd是echo“1111122223333344444555555666”| sed’s/,/6;s/,/12;s//,/18'在这里大声思考,但尝试用逗号替换空格,然后用``(空格)替换
,你真的想在输出中少一个
3
和一个
4
?谢谢你的快速回复。我能够解决这个问题。因为我必须插入逗号的字段是常量,所以我使用了multiplesed命令。因此,对于上面的示例,cmd是echo“1111122223333344444555555666”| sed’s/,/6;s/,/12;s/,/18'实际上,macOS的
sed
基于他们从FreeBSD获得的旧版本,它支持
-E
,但不支持
-r
。FreeBSD(和其他BSD)多年来一直同等支持这两种选项。
-E
等同于grep的
-E
选项(用于“扩展”RE)。我不确定GNU的人是从哪里想到在ERE中使用
-r
的。实际上,macOS的
sed
是基于他们从FreeBSD获得的旧版本,它支持
-E
,但不支持
-r
。FreeBSD(和其他BSD)多年来一直同等支持这两种选项。
-E
等同于grep的
-E
选项(用于“扩展”RE)。我不确定GNU的人是从哪里想到在ERE中使用
-r