AWK使用多个空格作为分隔符 [英] AWK to use multiple spaces as delimiter

查看:1566
本文介绍了AWK使用多个空格作为分隔符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下命令使用前两列连接两个文件.

I am using below command to join two files using first two columns.

awk 'NR==FNR{a[$1,$2]=substr($0,3);next} ($1,$2) in a{print $0, a[$1,$2] > "br0102_3.txt"}' br01.txt br02.txt

现在,默认情况下,AWk命令使用空格作为分隔符.但是我的文件可能在两个词之间包含单个空格,例如

Now, by default AWk command uses whitespaces as the separators. But my file may contain single space between two words, e.g.

文件1:

ABCD               TEXT1 TEXT2                     123123112312312312312312312312312312
BCDEFG             TEXT3TEXT4                      133123123123123123123123123125423423
QWERT              TEXT5TEXT6                      123123123123125456678786789698758567

文件2:

ABCD               TEXT1 TEXT2                     12312312312312312312312312312
BCDEFG             TEXT3TEXT4                      31242342342342342342342342343
MNHT               TEXT8 TEXT9                     31242342342342342342342342343

我希望结果文件为;

ABCD               TEXT1 TEXT2                     123123112312312312312312312312312312 12312312312312312312312312312
BCDEFG             TEXT3TEXT4                      133123123123123123123123123125423423 31242342342342342342342342343
QWERT              TEXT5TEXT6                      123123123123125456678786789698758567
MNHT               TEXT8 TEXT9                     31242342342342342342342342343

有任何提示吗?

推荐答案

awk支持将正则表达式作为FS的值,因此您可以指定至少匹配两个空格的正则表达式.类似于-F '[[:space:]][[:space:]]+'.

awk supports a regular expression as the value of FS so you can specify a regular expression that matches at least two spaces. Something like -F '[[:space:]][[:space:]]+'.

$ awk '{print NF}' File2
4
3
4

$ awk -F '[[:space:]][[:space:]]+' '{print NF}' File2
3
3
3

这篇关于AWK使用多个空格作为分隔符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆