英文:
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="," }
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\""
}
}
<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"
I'm assuming above that if your last row of input starts with a new keyn
value you want that line printed with a "0"
at the end, e.g.:
$ 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"
but it's obviously an easy tweak to do just about anything else in that case if that's not what you need.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论