[英]AWK - using element on next record GETLINE?
I got some problem with this basic data: 我在基本数据上遇到了一些问题:
DP;DG
67;
;10
;14
;14
;18
;18
;22
;65
68;
;0
;9
;25
;25
70;
that I'd like to transform on this kind of output: 我想对这种输出进行转换:
DP;DG
67;
;10
;14
;14
;18
;18
;22
;65;x
68;
;0
;9
;25
;25;x
70;
The "x" value comes if on the next line $1 exists or if $2 is null. 如果下一行$ 1存在或$ 2为空,则出现“ x”值。 From my understanding, I've to use getline but I don't get the way!
据我了解,我必须使用getline,但我没有办法! I've tried the following code:
我尝试了以下代码:
#!/bin/bash
file2=tmp.csv
file3=fin.csv
awk 'BEGIN {FS=OFS=";"}
{
print $0;
getline;
if($2="") {print $0";x"}
else {print $0}
}' $file2 > $file3
Seemed easy. 看起来很容易。 I don't mention the result, totally different from my expectation.
我没有提到结果,这与我的期望完全不同。
Some clue? 有一些线索吗? Is getline necessary on this problem?
getline是否有必要解决这个问题?
OK, I continue to test some code: 好的,我继续测试一些代码:
#!/bin/bash
file2=tmp.csv
file3=fin.csv
awk 'BEGIN {FS=OFS=";"}
{
getline var
if (var ~ /.*;$/) {
print $0";x";
print var;
}
else {
print $0;
print var;
}
}' $file2 > $file3
It's quite better, but still, all lines that should be marked aren't... I don't get why... 更好,但是仍然没有标记所有行...我不知道为什么...
give this one-liner a try: 尝试一下:
awk -F';' 'NR==FNR{if($1>0||!$2)a[NR-1];next}FNR in a{$0=$0";x"}7' file file
or 要么
awk -F';' 'NR==FNR{if($1~/\S/||$2).....
alternative one pass version 另一种通行证版本
$ awk -F\; 'NR>1 {printf "%s\n", (f && $2<0?"x":"")}
{f=$1<0; printf "%s", $0}
END {print ""}' file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.