如何给列之间的空间? [英] How to give space between columns?
问题描述
我有一个文本文件,如图below.I想给在第五列中的字符和号码之间的空间。我怎样才能做到这一点使用awk?
CXE 911 BV HEG A1029 53.030
BVF 912 CV LYA A1030 51.99所需的输出CXE 911 BV HEG一1029 53.030
BVF 912 CV LYA一1030 51.99
$ AWK'匹配($ 0 /([^ [:空间:]] + [[:空间:] ] +){4} [^ [:空间:]] /){
打印SUBSTR($ 0,1,RLENGTH),SUBSTR($ 0 RLENGTH + 1)}'文件
CXE 911 BV HEG一1029 53.030
BVF 912 CV LYA一1030 51.99
4以上是列一个你感兴趣的,即5日前的数量。如果你想在不同的领域工作,只是改变了明显的方式,这个数字。
如果您正在使用GAWK的是旧版本你需要添加--re间隔标记但对于新gawks RE-区间({4})默认情况下启用。
此外,这里有一个简短但GNU的awk的具体的解决方案,如果你preFER:
$的awk'{打印gensub(/(([^ [:空间:]] + [[:空间:]] +){4} [^ [:空间:] ])(*)/,\\\\ 1 \\\\ 3,)}文件
CXE 911 BV HEG一1029 53.030
BVF 912 CV LYA一1030 51.99
您可以用一对子(做任何类似AWK)■但它的丑陋,所以我会用比赛()/ SUBSTR()对于那些awks。
最后,如果像有些人已经发布,你想要一个解决方案,以每行21字符后添加一个空格,而不是在每行第5场的第一个字符在那之后只希望是:
$ awk的子(/ {21} /,&安培;。)文件
CXE 911 BV HEG一1029 53.030
BVF 912 CV LYA一1030 51.99
I have a text file as shown below.I would like to give a space between the character and number in the fifth column. How can I do this with awk?
cxe 911 bv heg A1029 53.030
bvf 912 cv lya A1030 51.99
Desired output
cxe 911 bv heg A 1029 53.030
bvf 912 cv lya A 1030 51.99
$ awk 'match($0,/([^[:space:]]+[[:space:]]+){4}[^[:space:]]/) {
print substr($0,1,RLENGTH), substr($0,RLENGTH+1) }' file
cxe 911 bv heg A 1029 53.030
bvf 912 cv lya A 1030 51.99
The "4" above is the number of columns before the one you're interested in, i.e. the 5th. If you want to operate on a different field, just change that number in the obvious way.
If you're using an older version of gawk you'd need to add the --re-interval flag but for newer gawks RE-intervals ({4}) are enabled by default.
Also, here's a briefer but GNU-awk specific solution if you prefer:
$ awk '{print gensub(/(([^[:space:]]+[[:space:]]+){4}[^[:space:]])(.*)/,"\\1 \\3","")}' file
cxe 911 bv heg A 1029 53.030
bvf 912 cv lya A 1030 51.99
You can do similar in any awk using a pair of sub()s but it's ugly so I'd use the match()/substr() for those awks.
Finally if, as some others have posted, you want a solution to add a space after the 21st character on each line rather than after the first character in the 5th field on each line then that'd just be:
$ awk 'sub(/.{21}/,"& ")' file
cxe 911 bv heg A 1029 53.030
bvf 912 cv lya A 1030 51.99
这篇关于如何给列之间的空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!