简体   繁体   English

awk blues ..怎么了?

[英]awk blues.. What's wrong?

I have to read from a file 'main.csv' and print the output as below: 我必须从文件“ main.csv”中读取并打印输出,如下所示:

Set 4,1095,Set 1 or 4
Set 2,18277,Set 2 or 3
Set 1 or 4,Set 4,944
Set 2,1373,Set 2 or 3
Set 4,83,Set 1 or 4
Set 2,14356,Set 2 or 3
Set 2,14800,Set 2 or 3

I tried below with respective outputs: 我在下面尝试了各自的输出:

awk 'BEGIN {FS=","}{OFS=","}{print $12,$15}' main.csv
Set 4,1095
Set 2,18277
Set 4,944
Set 2,1373
Set 4,83

awk 'BEGIN {FS=","}{OFS=","}{if($12 == "Set 1" || $12 == "Set 4"){print "Set 1 or 4",$12,$15}else{print "Set 2 or 3",$12,$15}}' main.csv
Set 1 or 4,Set 4,1095
Set 2 or 3,Set 2,18277
Set 1 or 4,Set 4,944
Set 2 or 3,Set 2,1373
Set 1 or 4,Set 4,83
Set 2 or 3,Set 2,14356
Set 2 or 3,Set 2,14800

awk 'BEGIN {FS=","}{OFS=","}{if($12 == "Set 1" || $12 == "Set 4"){print $12,$15,"Set 1 or 4"}else{print $12,$15,"Set 2 or 3"}}' main.csv
,Set 1 or 4
,Set 2 or 3
,Set 1 or 4
,Set 2 or 3
,Set 1 or 4
,Set 2 or 3
,Set 2 or 3

First 2 "awk"s run fine, then why not the 3rd one ? 前两个“ awk”运行良好,然后为什么不运行第三个?

main.csv has 15 comma separated values per line with 12th value as either of below: main.csv每行有15个逗号分隔的值,第12个值如下所示:

Set 1
Set 2
Set 3
Set 4

PS: I'm using bash on Solaris 10 PS:我在Solaris 10上使用bash

I'm still high on "awk" issues, below is what I noticed... not sure why it's happening (I'm getting this on both Solaris 10 and RHEL 5) 我对“ awk”问题仍然很关注,以下是我注意到的问题……不确定为什么会发生(我在Solaris 10和RHEL 5上都看到了)

awk -F, '{print "Var13->"$13,"\t","Var12->"$12,"\t","Var15 ->"$15}' < main.csv
Var13->0         Var12->Set 4    Var15 ->1095
Var13->1631      Var12->Set 2    Var15 ->18277
Var13->0         Var12->Set 4    Var15 ->944
Var13->2832      Var12->Set 2    Var15 ->1373
Var13->0         Var12->Set 4    Var15 ->83

awk -F, '{print "Var13 & Var15->"$13,"\t","Var12->"$12,"\t","Var15 ->"$15}' < main.csv
Var13 & Var15->0         Var12->Set 4    Var15 ->1095
Var13 & Var15->1631      Var12->Set 2    Var15 ->18277
Var13 & Var15->0         Var12->Set 4    Var15 ->944
Var13 & Var15->2832      Var12->Set 2    Var15 ->1373
Var13 & Var15->0         Var12->Set 4    Var15 ->83


awk -F, '{print "Var13 & Var15->"$13,$15"\t","Var12->"$12,"\t","Var15 ->"$15}' < main.csv
or
awk -F, '{print "Var13 & Var15->"$13,$15,"\t","Var12->"$12,"\t","Var15 ->"$15}' < main.csv
or
awk -F, '{print "Var13 & Var15->"$13,"\t",$15,"\t","Var12->"$12,"\t","Var15 ->"$15}' < main.csv

ar13 &  Var12->Set 4    Var15 ->1095
ar13 &  Var12->Set 2 27 Var15 ->18277
ar13 &  Var12->Set 4    Var15 ->944
ar13 &  Var12->Set 2 73 Var15 ->1373
ar13 &  Var12->Set 4    Var15 ->83
ar13 &  Var12->Set 2 56 Var15 ->14356

As you can see last 3 runs had eaten up the letter from "Var13" output and added some junk info. 如您所见,最近3次运行已经耗尽了“ Var13”输出中的字母,并添加了一些垃圾信息。

No need to vote for this, I just wanted to paste in some formatted code. 无需为此投票,我只想粘贴一些格式化的代码。

Your program is getting big enough to deserve its own code file where it can be formatted in a civilized fashion and compared to other versions. 您的程序已经足够大,可以保留自己的代码文件,可以在其中以文明的方式对其进行格式化,并与其他版本进行比较。 When we do this we can easily see that you are assigning OFS in an action on every input line. 当我们执行此操作时,我们可以很容易地看到您正在对每个输入行的操作中分配OFS。 No harm done, but it's obviously unintended so you lose 1337 points. 没有伤害,但显然是意外的,因此您损失了1337分。 With that fixed we move on. 固定下来之后,我们继续前进。

You can't diff a one-liner and learn anything. 你不能只差一线而学到任何东西。 Running diff(1) on the nicely formatted code shows that only the output lines have changed, and so both programs are equally valid. 在格式良好的代码上运行diff(1)显示仅更改了输出行,因此两个程序均有效。 You must have made the third run under different conditions. 您必须在不同的条件下进行第三次运行。 Perhaps a different input file? 也许是不同的输入文件?

BEGIN  {
  FS  = ","
  OFS = ","
}
{ if($12 == "Set 1" || $12 == "Set 4") {
    print "Set 1 or 4",$12,$15
  } else {
    print "Set 2 or 3",$12,$15
  }
}

BEGIN {
  FS  = ","
  OFS = ","
}
{ if($12 == "Set 1" || $12 == "Set 4") {
    print $12,$15,"Set 1 or 4"
  } else {
    print $12,$15,"Set 2 or 3"
  }
}

My money's on you having control-Ms at the end of your ".csv" file lines causing the first part of the displayed lines to get hidden. 我的钱全靠您在“ .csv”文件行的末尾具有控制权-M导致所显示行的第一部分被隐藏。

This corruption is brought to you at no extra cost by the people who brought you Windows Vista, etc. 带给您Windows Vista等的人员免费为您带来这种损坏。

Run "dos2unix" on your ".csv" file and then try again. 在“ .csv”文件上运行“ dos2unix”,然后重试。

If that doesn't work, run "where awk" and "awk --version" and tell us the result. 如果那不起作用,请运行“ where awk”和“ awk --version”,然后告诉我们结果。 On Solaris you must use /usr/xpg4/bin/awk or nawk, do not use /bin/awk or /usr/bin/awk as those are both old, broken awk. 在Solaris上,您必须使用/ usr / xpg4 / bin / awk或nawk,请勿使用/ bin / awk或/ usr / bin / awk,因为它们都是旧的,损坏的awk。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM