如何使用壳

问题描述:

我已经数据如下如何使用壳

11111 22222 33333 44444      55555   66666 

我想的输出数据通过转换如下

11111,22222,3333,4444      ,55555   ,66666 

我使用tr命令试图插入一个分隔符每隔第n列请在4444或55555之后使用逗号。注意:4444和5555之间有很多空格。是否可以用逗号填充第6,12,127,46等列的空格?

+0

只是想大声这里,但尝试用逗号替换空格然后更换后',,'用''(空间) – Dominik

+3

你真的想要一个少' 3'和一个少于'4'的输出? – jas

+0

感谢您的快速回复。我能解决这个问题。因为我不得不插入逗号的字段是常量,所以我使用了多个sed命令。所以对于上面的例子,cmd是 – Aires69

如果您的实际Input_file与显示的样本相同,那么以下内容可能也会对您有所帮助。

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. 

难道这只是做你正在寻找什么? (猜测中所需输出的失踪4人是一个错字)

sed -E 's/ ([0-9])/,\1/g' data 

(使用-r而不是-E,如果你是在Mac)。

+0

实际上,macOS的' sed'基于他们从FreeBSD抓取的旧版本,它支持'-E'而不是'-r'。 FreeBSD(和其他BSD)多年来一直支持这两种选择。 '-E'旨在等同于grep的'-E'选项(对于“扩展”RE)。我不确定GNU人们在ERE中使用'-r'的想法。 – ghoti