awk – 如果匹配到模式,添加一个具有下一行中找到的值的列。

huangapple go评论65阅读模式
英文:

awk - Adding a column with value found in next line, if patter is matched

问题

我正在尝试将此文件转换为:

keyn,assert,code,code
"11","1","25","21"
"11","1","40","0"
"12","0","35","0"
"12","0","10","0";

即对于第一个出现的 `keyn`,如果 `assert == "1"`,在行末打印下一个寄存器的 `$3` 并删除该行,如果 `assert == "0"`,在行末只打印 `"0"`。

到目前为止,我尝试过以下方法,但没有成功:

awk  'BEGIN{OFS=FS=","} {num[NR] = $3; if ($2=="1") {print $0, num[i+1]} else {print $0,"0" }}' file
英文:

I am trying to transform this file

keyn,assert,code
"11","1","25"
"11","1","21"
"11","1","40"
"12","0","35"
"12","0","10"

to this one:

keyn,assert,code,code
"11","1","25","21"
"11","1","40","0"
"12","0","35","0"
"12","0","10","0"

i.e. for the first instance of keyn, if assert == "1", print $3 of the next register at the end of the line and delete that line, and if assert == "0", print just "0" at the end of the line.

Until now, I tried this, without success:

awk  'BEGIN{OFS=FS=","} {num[NR] = $3; if ($2=="1") {print $0, num[i+1]} else {print $0,"0" }}' file

答案1

得分: 2

我会使用几个变量来传递值到下一条记录:

英文:

I'd use a couple of variables to carry values over to the next record:

awk '
    BEGIN {FS = OFS = ","; zero = "\"0\""}
    NR == 1 {print $0, "code"; next}
    prev != "" {
        print prev, $1 == key ? $3 : zero
        prev = ""
        if ($1 == key) next
    }
    {key = $1}
    $2 ~ /1/ {prev = $0; next}
    {print $0, zero}
' file

This works on the sample data you show. No guarantees.

答案2

得分: 1

我认为您想要的翻译是:

"我认为这是您想要做的事情:

$ cat tst.awk
BEGIN { FS=OFS="," }
NR == 1 {
    print $0, "code"
    next
}
$1 != prev {
    prev = $1
    if ( $2 == "\"\"" 1 "\"\"" ) {
        saved = $0
        next
    }
}
{
    print ( saved == "\"\"" ? $0 OFS "\"0\"" : saved OFS $3 )
    saved = "\"\""
}
END {
    if ( saved != "\"\"" ) {
        print saved OFS "\"0\""
    }
}

$ awk -f tst.awk file
keyn,assert,code,code
"11","1","25","21"
"11","1","40","0"
"12","0","35","0"
"12","0","10","0"

我假设您的输入的最后一行以新的keyn值开头时,您希望在该行末尾打印一个"0",例如:

$ cat file
keyn,assert,code
"11","1","25"
"11","1","21"
"11","1","40"
"12","0","35"
"12","0","10"
"13","1","10"

<p>

$ awk -f tst.awk file
keyn,assert,code,code
"11","1","25","21"
"11","1","40","0"
"12","0","35","0"
"12","0","10","0"
"13","1","10","0"

但如果这不是您需要的内容,显然很容易进行调整以满足其他要求。"

英文:

I think this is what you're trying to do:

$ cat tst.awk
BEGIN { FS=OFS=&quot;,&quot; }
NR == 1 {
    print $0, &quot;code&quot;
    next
}
$1 != prev {
    prev = $1
    if ( $2 == &quot;\&quot;&quot; 1 &quot;\&quot;&quot; ) {
        saved = $0
        next
    }
}
{
    print ( saved == &quot;&quot; ? $0 OFS &quot;\&quot;0\&quot;&quot; : saved OFS $3 )
    saved = &quot;&quot;
}
END {
    if ( saved != &quot;&quot; ) {
        print saved OFS &quot;\&quot;0\&quot;&quot;
    }
}

<p>

$ awk -f tst.awk file
keyn,assert,code,code
&quot;11&quot;,&quot;1&quot;,&quot;25&quot;,&quot;21&quot;
&quot;11&quot;,&quot;1&quot;,&quot;40&quot;,&quot;0&quot;
&quot;12&quot;,&quot;0&quot;,&quot;35&quot;,&quot;0&quot;
&quot;12&quot;,&quot;0&quot;,&quot;10&quot;,&quot;0&quot;

I'm assuming above that if your last row of input starts with a new keyn value you want that line printed with a &quot;0&quot; at the end, e.g.:

$ cat file
keyn,assert,code
&quot;11&quot;,&quot;1&quot;,&quot;25&quot;
&quot;11&quot;,&quot;1&quot;,&quot;21&quot;
&quot;11&quot;,&quot;1&quot;,&quot;40&quot;
&quot;12&quot;,&quot;0&quot;,&quot;35&quot;
&quot;12&quot;,&quot;0&quot;,&quot;10&quot;
&quot;13&quot;,&quot;1&quot;,&quot;10&quot;

<p>

$ awk -f tst.awk file
keyn,assert,code,code
&quot;11&quot;,&quot;1&quot;,&quot;25&quot;,&quot;21&quot;
&quot;11&quot;,&quot;1&quot;,&quot;40&quot;,&quot;0&quot;
&quot;12&quot;,&quot;0&quot;,&quot;35&quot;,&quot;0&quot;
&quot;12&quot;,&quot;0&quot;,&quot;10&quot;,&quot;0&quot;
&quot;13&quot;,&quot;1&quot;,&quot;10&quot;,&quot;0&quot;

but it's obviously an easy tweak to do just about anything else in that case if that's not what you need.

huangapple
  • 本文由 发表于 2023年5月30日 05:38:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360451.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定