我的数据如下
11111 22222 33333 44444 55555 66666
我想输出数据如下
11111,22222,3333,4444 ,55555 ,66666
我尝试使用逗号转换空格来使用tr命令,但是在4444 OR 55555之后得到所有逗号。注意:4444和5555之间有很多空格。是否可以在6号,12号填充空格,17号,46号等用逗号列?
答案 0 :(得分:4)
这不就是你要找的吗? (猜测你想要的输出中缺少4个是错字)
sed -E 's/ ([0-9])/,\1/g' data
(如果你在Mac上,请使用-r
代替-E
。
答案 1 :(得分:0)
如果您的实际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.