如何插入不同的分隔符,在外壳两列之间 [英] How to insert a different delimiter in between two columns in shell
本文介绍了如何插入不同的分隔符,在外壳两列之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经一个文件,如下
ABc def|0|0|0| 1 | 2| 9|
0 2930|0|0|0|0| 1 | 2| 9|
现在,我想的第一列具有相同的定界符分割
Now, i want to split the first column with the same delimiter.
输出:
ABc|def|0|0|0| 1 | 2| 9|
0|2930|0|0|0|0| 1 | 2| 9|
请帮我出的awk。
推荐答案
您可以使用 SED
此:
$ sed 's/ /|/' file
ABc|def|0|0|0| 1 | 2| 9|
0|2930|0|0|0|0| 1 | 2| 9|
它的定义方式,它只是替换第一空间 |
,这正是你需要的。
使用 AWK
这是一个有点长:
$ $ awk 'BEGIN{FS=OFS="|"}{split($1, a, " "); $1=a[1]"|"a[2]}1' file
ABc|def|0|0|0| 1 | 2| 9|
0|2930|0|0|0|0| 1 | 2| 9|
definining输入和输出的字段分隔符为之后|
,它拆分基于太空的第一场。然后打印行了。
After definining input and output field separator as |
, it splits the first field based on space. Then prints the line back.
这篇关于如何插入不同的分隔符,在外壳两列之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文