用awk NR可变困惑:文件格式 [英] File Formatting : Confusion using awk NR Variable
问题描述
我是新来的awk编程和对使用NR变量的有点糊涂了。
我的code是...
的awk'BEGIN {K = NR;} {printf的(%s%s%S%S \\ n,$ K,$(K + 1),$(K +2),$(K + 3))}'AUTH_DATA
$猫AUTH_DATA
6262 6530 6661 3162 6364 6264 6561 3430 3033 3332 6536 3139 6230 6261 61
30 3637 0A00 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000
输出:
6262 6530 6661 3162 6364 6264 6561 3430 3033 3332 6536 3139 6230 6261 6130
3637 0A00 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
0000 0000 6262 6530 6661
但我想要的是输出应该是格式为:
6262 6530 6661 3162 6364 6264 6561 3430
3033 3332 6536 3139 6230 6261 6130 3637
0A00 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000
我猜你在找什么是NF,不是NR。
从联机帮助:
NR的输入记录迄今所看到的总数。
块引用>NR是实际的行号,但在这个问题上,你想要做现场IDX,不是行的某些伎俩。
另外,我还以为你输入的数据应该在文件'AUTH_DATA'一条线,对吧?
如果是这样,你可以尝试
的awk'{为(i = 1; I< = NF;我++),如果(I%8 == 0)打印$ I;其他的printf $ I}'AUTH_DATA
检查下面的测试:
肯特$回声6262 6530 6661 3162 6364 6264 6561 3430 3033 3332 6536 3139 6230 6261 6130 3637 0A00 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000| \\
AWK'{为(i = 1; I< = NF;我++),如果(I%8 == 0)打印$ I;其他的printf $ I}
6262 6530 6661 3162 6364 6264 6561 3430
3033 3332 6536 3139 6230 6261 6130 3637
0A00 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000和背部的问题,如果你只是想要做的格式,xargs的就够了。见下图:
肯特$回声6262 6530 6661 3162 6364 6264 6561 3430 3033 \\
3332 6536 3139 6230 6261 \\
6130 3637 0A00 0000 0000 \\
0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000| xargs的-N 8输出:
6262 6530 6661 3162 6364 6264 6561 3430
3033 3332 6536 3139 6230 6261 6130 3637
0A00 0000 0000 0000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0000 0000您可以
猫yourFile | xargs的-N 8
或xargs的-N 8 -a yourfile
I am new to awk programming and little confused about the use of NR variable ..
My code is ...
awk 'BEGIN {k=NR;}{printf("%s %s %s %s\n",$k,$(k+1),$(k+2),$(k+3))}' auth_data
$ cat auth_data
6262 6530 6661 3162 6364 6264 6561 3430 3033 3332 6536 3139 6230 6261 61 30 3637 0A00 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
Output :
6262 6530 6661 3162 6364 6264 6561 3430 3033 3332 6536 3139 6230 6261 6130 3637 0A00 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 6262 6530 6661
But what I want is that output should be in this format :
6262 6530 6661 3162 6364 6264 6561 3430 3033 3332 6536 3139 6230 6261 6130 3637 0A00 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
解决方案I guess what you are looking for is NF, not NR.
from Manpage:
NR The total number of input records seen so far.
NR is actual line number, but in this problem, you want to do some trick on field idx, not lines.
Also, I thought your input data should be in one line in file 'auth_data', right?
if it is so, you could try
awk '{for(i=1;i<=NF;i++)if(i%8==0)print $i;else printf $i" "}' auth_data
check the test below:
kent$ echo "6262 6530 6661 3162 6364 6264 6561 3430 3033 3332 6536 3139 6230 6261 6130 3637 0A00 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000"|\ awk '{for(i=1;i<=NF;i++)if(i%8==0)print $i;else printf $i" "}' 6262 6530 6661 3162 6364 6264 6561 3430 3033 3332 6536 3139 6230 6261 6130 3637 0A00 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
And back to the problem, if you just want to do the formatting, xargs is enough. see below:
kent$ echo "6262 6530 6661 3162 6364 6264 6561 3430 3033\ 3332 6536 3139 6230 6261\ 6130 3637 0A00 0000 0000 \ 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000"|xargs -n8
output:
6262 6530 6661 3162 6364 6264 6561 3430 3033 3332 6536 3139 6230 6261 6130 3637 0A00 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000
you can
cat yourFile|xargs -n8
orxargs -n8 -a yourfile
这篇关于用awk NR可变困惑:文件格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!