AWK没有捕捉第一线/分离器 [英] awk not capturing first line / separator

查看:96
本文介绍了AWK没有捕捉第一线/分离器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白以下行为:

这是一个文本文件:

example.txt
12345   4321    hello hello this is a test  blobb
14324   2131    another test , incoming !   blubb
52341   1231    last test now shutting down bla
...

它由文字x行,4制表符分隔栏每个。我只需要前三,所以我用AWK(第一时间):

It consists of x rows of text, 4 tab-separated columns each. I only need the first three, so I used awk (for the first time):

awk '{FS="\t"; OFS="\t"; print $1,$2,$3}' < example.txt > excerpt.txt

结果是这样的:

excerpt.txt
12345   4321    hello
14324   2131    another test , incoming !
52341   1231    last test now shutting down
...

中的第一项不包含完整的第三列,和印刷 $ 1,$ 2,$ 3,$ 4'/ code>给 12345 4321你好你好第一行。所以,显然它在分离空白(后两者的第一和第二个你好),而不是在标签。我查了一下,如果卡在那里悄悄,但事实并非如此:

The first entry does not contain the full third column, and printing $1,$2,$3,$4 gives 12345 4321 hello hello for the first row. So, apparently it separates at whitespace (both after the first and the second hello), and not at tab. I checked if a tab snuck in there, but that was not the case:

我觉得这是非常混乱的,因为它可以正确处理所有其他行。

I find this very confusing, since it works correctly for all other rows.

推荐答案

您要设置在每行的字段分隔符。然后, AWK 读取记录(行),但它是当光标到达的方式,所以第一次你设置它是是的太晚的应用到第一条记录。

You are setting the field separators on every line. Then, awk reads the record (line) but in the way it was when the cursor reached it, so the first time you are setting it is is too late to apply to the first record.

由于默认的分隔符是空间,在第一行,它使用它。然后,从第二记录,它考虑到了什么是previously设置

Since the default field separator is a space, on the first line it uses it. Then, from the second record, it takes into account what was previously set.

您需要设置它无论是在 BEGIN 块或之前(效果是一样的):

You need to set it either in the BEGIN block or before (the effect is the same):

awk 'BEGIN{FS=OFS="\t"} {print $1,$2,$3}' example.txt > excerpt.txt

awk -F"\t" -v OFS="\t" '{print $1,$2,$3}' example.txt > excerpt.txt

另外,您也可以在重新编译的东西,如 $ 1 = $ 1 的记录。这reinter $ P $点基于当前字段分隔符的记录。因此,这应该工作以及

Alternatively, you can also "recompile" the record with something like $1=$1. This reinterprets the record based on the current field separators. So this should work as well:

awk '{FS=OFS="\t"; $0=$0; print $1,$2,$3}' example.txt > excerpt.txt

测试

测试的最后一件事,在重新编译场。

Test

Testing the last thing, on recompiling the field.

$ cat a
hello this      is me and
here we are     doing some awk
$ awk '{FS="\t"; print $2}' a
this
doing some awk
$ awk '{FS="\t"; $0=$0; print $2}' a
is me and
doing some awk

这篇关于AWK没有捕捉第一线/分离器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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