awk中如何获取子串
这是输入文件的一行:
FOO BAR 0.40 0.20 0.40 0.50 0.60 0.80 0.50 0.50 0.50 -43.00 100010101101110101000111010
以及一个 awk 命令,用于检查某个位置在第 13 列是否为“1”或“0” 类似于:
awk -v values="${values}" '{if (substr($13,1,1)==1) printf values,$1,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13}' foo.txt > bar.txt
values 变量有效,但我只想在上面的示例中检查第一位是否等于“1”。
编辑
好的,所以我想我的问题不是很清楚。 substr方法中的“$13”实际上是位串。因此,这个 awk 想要传递 foo.txt 中“$13”列位串的“1”位置处有“1”的所有行。希望这能澄清事情。
编辑2
好吧,让我简单地分解一下。上面的代码是示例,因此输入行是许多行之一。因此,并非所有行在位置 8 处都有 1。我仔细检查了某个位置是否同时出现了这两种情况,以便在任何情况下我都应该得到一些输出。问题是,在所有行中,它在我选择的位置上找不到任何“1”,但是当我说它必须找到“0”时,它会返回所有行。
This is one line of the input file:
FOO BAR 0.40 0.20 0.40 0.50 0.60 0.80 0.50 0.50 0.50 -43.00 100010101101110101000111010
And an awk command that checks a certain position if it's a "1" or "0" at column 13
Something like:
awk -v values="${values}" '{if (substr($13,1,1)==1) printf values,$1,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13}' foo.txt > bar.txt
The values variable works, but i just want in the above example to check if the first bit if it is equal to "1".
EDIT
Ok, so I guess I wasn't very clear in my question. The "$13" in the substr method is in fact the bitstring. So this awk wants to pass all the lines in foo.txt that have a "1" at position "1" of the bitstring at column "$13". Hope this clarifies things.
EDIT 2
Ok, let me break it down real easy. The code above are examples, so the input line is one of MANY lines. So not all lines have a 1 at position 8. I've double checked to see if a certain position has both occurences, so that in any case I should get some output. Thing is that in all lines it doesn't find any "1"'s on the posistions that I choose, but when I say that it has to find a "0" then it returns me all lines.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)