awk与零比较失败?

时间:2013-09-26 10:16:20

标签: awk

我使用类似于以下示例的制表符分隔文件:

java/awt/Graphics2D/results.json    -0.0007254401050271753
java/util/Collection/results.json   0.03368847875680803
java/util/Map$Entry/results.json    0.0
java/util/Calendar/results.json 0.043459993814160214
java/io/ObjectInputStream/results.json  0.004388257233926218
java/util/Set/results.json  0.027563163785955025
java/lang/NoClassDefFoundError/results.json 0.0
java/io/ByteArrayOutputStream/results.json  0.005683957785502014
javax/xml/parsers/DocumentBuilderFactory/results.json   0.0
java/util/HashSet/results.json  0.00041155025194439254
java/lang/reflect/Field/results.json    0.00890593203598622
java/io/BufferedInputStream/results.json    -0.002988394526148186

我想打印零值的行,但我无法让它工作。这是我试过的:

awk '$2 == 0.0 { print $1 $2; }'
awk '$2 ~ /^0.0$/ { print $1 $2; }'
awk '! $2 { print $1 $2; }'

但他们都没有回报任何东西。当我用<=替换相等时,我得到了结果。 “0.0”的内部表示是否为零或者我在这里缺少什么?

[编辑]

正如第一个评论和答案所指出的那样,我的方法确实适用于给定的例子。由于我仍无法获得所需的文件结果,因此我现在提供完整的文件:

type    difference
java/util/zip/ZipEntry/results.json 0.02811540961236958
java/io/File/results.json   0.04152288811788907
javax/xml/parsers/SAXParser/results.json    0.0
java/lang/reflect/Constructor/results.json  0.0009129948091427531
java/io/Reader/results.json -0.00043988180335940985
org/w3c/dom/NodeList/results.json   0.006532216750947728
java/io/FileOutputStream/results.json   0.007256000537915197
java/lang/StringBuffer/results.json 0.007679996705180603
java/util/Timer/results.json    0.016366219196432108
java/util/Stack/results.json    0.006318970191216411
java/util/HashMap/results.json  0.007887416771271583
java/util/regex/Pattern/results.json    0.0
java/lang/SecurityManager/results.json  0.022447250291888987
java/lang/Integer/results.json  0.0009540861574682813
java/lang/reflect/Method/results.json   0.0034659896518139943
java/awt/Graphics2D/results.json    -0.0007254401050271753
java/util/Collection/results.json   0.03368847875680803
java/util/Map$Entry/results.json    0.0
java/util/Calendar/results.json 0.043459993814160214
java/io/ObjectInputStream/results.json  0.004388257233926218
java/util/Set/results.json  0.027563163785955025
java/lang/NoClassDefFoundError/results.json 0.0
java/io/ByteArrayOutputStream/results.json  0.005683957785502014
javax/xml/parsers/DocumentBuilderFactory/results.json   0.0
java/util/HashSet/results.json  0.00041155025194439254
java/lang/reflect/Field/results.json    0.00890593203598622
java/io/BufferedInputStream/results.json    -0.002988394526148186
java/util/Random/results.json   0.011227287619054893
java/util/TimeZone/results.json 0.024665400046989694
java/lang/Class/results.json    0.07421947121451256
java/util/concurrent/atomic/AtomicReference/results.json    0.10331150665256295
java/io/Writer/results.json -0.009594193559632203
java/text/MessageFormat/results.json    0.0
java/util/concurrent/locks/Lock/results.json    0.0
java/lang/Process/results.json  0.03305842089211253
java/awt/Graphics/results.json  -0.014799823847054583
java/io/ObjectInput/results.json    0.0043643251190484955
java/io/DataOutputStream/results.json   0.028180125582963456
java/net/ServerSocket/results.json  0.0
java/lang/CharSequence/results.json 0.0014546156264344523
org/w3c/dom/Document/results.json   0.044205828635986544
java/lang/ref/SoftReference/results.json    0.0
java/util/Date/results.json 0.03739704095408358
java/util/BitSet/results.json   0.01595348836239574
java/beans/PropertyChangeEvent/results.json 0.0736002108400371
java/lang/Throwable/results.json    0.015442674292838077
org/xml/sax/InputSource/results.json    0.00504041008343914
java/util/concurrent/atomic/AtomicBoolean/results.json  0.0
java/util/Enumeration/results.json  0.0
java/io/OutputStream/results.json   0.03207385133784557
javax/swing/JMenuItem/results.json  0.007580588697566837
java/util/LinkedHashSet/results.json    -0.016756794274537468
java/util/ListIterator/results.json -0.020853784272820364
java/util/PropertyResourceBundle/results.json   0.0
java/util/TreeSet/results.json  0.044817278597274135
java/io/DataInputStream/results.json    0.053026460924931906
java/sql/PreparedStatement/results.json 0.09681700283436634
java/sql/Connection/results.json    0.13935867828592935
org/xml/sax/helpers/AttributesImpl/results.json 0.0
java/lang/Iterable/results.json 0.0
java/util/Hashtable/results.json    0.002679420570760871
org/w3c/dom/Text/results.json   0.0
java/util/Properties/results.json   0.11327875446939961
java/util/concurrent/atomic/AtomicLong/results.json -0.010251951289681704
org/xml/sax/SAXParseException/results.json  0.07333548447668281
java/lang/Boolean/results.json  0.0047512888249205965
java/net/URL/results.json   0.10408154020680904
java/io/PrintWriter/results.json    0.03420358878056462
java/util/ResourceBundle/results.json   0.0
java/lang/Enum/results.json 0.032303227721100725
java/util/SortedMap/results.json    0.08744496853140682
java/nio/ByteBuffer/results.json    0.10982055662447976
javax/swing/JInternalFrame/results.json 0.0
javax/swing/JPanel/results.json 0.029944242845839852
java/lang/Double/results.json   0.012100663817329216
java/sql/ResultSet/results.json 0.0466592420153662
java/util/LinkedList/results.json   0.024632903602906486
org/w3c/dom/Node/results.json   0.10037281423252875
org/w3c/dom/NamedNodeMap/results.json   0.0
java/util/regex/Matcher/results.json    0.026061882971817107
java/lang/Runnable/results.json 0.0
javax/xml/parsers/SAXParserFactory/results.json 0.0
java/io/InputStream/results.json    0.016656317939729548
java/util/Map/results.json  0.037111411767002944
java/io/BufferedReader/results.json 0.04313808075138814
java/util/LinkedHashMap/results.json    0.12558632507110923
java/util/Iterator/results.json 0.001078276973821346
java/io/StringWriter/results.json   -0.01217951196013567
java/lang/String/results.json   0.10887317528796281
org/xml/sax/XMLReader/results.json  0.0019673750860798034
java/lang/Exception/results.json    0.02072166912805773
java/lang/StringBuilder/results.json    -0.008138028734336222
java/lang/ClassLoader/results.json  0.13500196641014406
java/util/StringTokenizer/results.json  0.0
org/xml/sax/Attributes/results.json 0.037444901309106715
java/util/Locale/results.json   -0.002155567254523705
org/w3c/dom/ProcessingInstruction/results.json  0.0
java/awt/Container/results.json 0.17531504176646578
org/w3c/dom/Comment/results.json    0.18453512321427057
java/net/Socket/results.json    0.0015579907762824918
java/lang/Thread/results.json   0.13990245294661952
java/awt/Component/results.json 0.006227573200654146
java/util/List/results.json 0.03846009954116536
java/lang/Object/results.json   0.1044811092147323
java/util/Dictionary/results.json   0.008123421708587264
java/lang/Long/results.json -0.03193234419266067
javax/swing/text/JTextComponent/results.json    0.021581000121873306
javax/xml/transform/Transformer/results.json    0.0
javax/swing/JButton/results.json    -0.012595220962732578
java/util/ArrayList/results.json    0.018424764828314788
javax/xml/namespace/QName/results.json  0.0
org/w3c/dom/Element/results.json    0.08394591221121561
java/io/FilterInputStream/results.json  0.31143660022857694
java/awt/geom/Point2D/results.json  -0.01419500947802077
javax/xml/parsers/DocumentBuilder/results.json  0.08379309434821058
java/util/Vector/results.json   0.03531433307692444
java/awt/geom/AffineTransform/results.json  0.02444537705524863
java/io/RandomAccessFile/results.json   0.013654524542530022
java/io/PrintStream/results.json    -0.0054164506429096315
java/net/URI/results.json   0.1156988886086383
java/util/TreeMap/results.json  -0.009435345113169347
java/math/BigInteger/results.json   0.13314587395291244
java/lang/ThreadLocal/results.json  0.0018628454065079403
java/lang/ref/ReferenceQueue/results.json   0.0
java/io/ObjectOutputStream/results.json 0.0017384824717933123

我意识到我这里有一个标题 - 这可能导致麻烦吗?当我用grep删除标题时,它仍然无效。

4 个答案:

答案 0 :(得分:4)

您的输入文件是在Windows上创建的,因此在每一行的末尾都有虚假控件-Ms。您已经发现可以在输入文件上运行dos2unix来修复它。

除此之外:

Awk不适用于行,它适用于记录。 awk的默认记录分隔符(RS)是一个换行符,所以它看起来像awk在线上工作,但这只是一个默认行为。您可以告诉awk使用其他字符来分隔记录但是对于大多数awks,RS只能是单个字符,因此当Microsoft决定引入2个字符的行尾字符串<CR><LF>时,则会创建一个问题

使用GNU awk(可能还有其他人的COUPLE),您可以为RS指定一个多字符正则表达式,这样他们只需将RS设置为<CR><LF>即可轻松处理微软的愚蠢,但对于其他问题,您可以必须将微软的多字符行分隔符字符串转换为awks RS可以设置为的单个字符记录分隔符,例如通过运行dos2unix去掉控件-Ms并只留下换行符,以便默认RS按原样处理它。

答案 1 :(得分:1)

试试这一行:

awk '$2*1==0' file

awk '!($2*1)' file

输出:

kent$  awk '$2*1==0' f
java/util/Map$Entry/results.json    0.0
java/lang/NoClassDefFoundError/results.json 0.0
javax/xml/parsers/DocumentBuilderFactory/results.json   0.0

修改

完整档案的输出:

kent$  awk '$2*1==0' f   
type    difference
javax/xml/parsers/SAXParser/results.json    0.0
java/util/regex/Pattern/results.json    0.0
java/util/Map$Entry/results.json    0.0
java/lang/NoClassDefFoundError/results.json 0.0
javax/xml/parsers/DocumentBuilderFactory/results.json   0.0
java/text/MessageFormat/results.json    0.0
java/util/concurrent/locks/Lock/results.json    0.0
java/net/ServerSocket/results.json  0.0
java/lang/ref/SoftReference/results.json    0.0
java/util/concurrent/atomic/AtomicBoolean/results.json  0.0
java/util/Enumeration/results.json  0.0
java/util/PropertyResourceBundle/results.json   0.0
org/xml/sax/helpers/AttributesImpl/results.json 0.0
java/lang/Iterable/results.json 0.0
org/w3c/dom/Text/results.json   0.0
java/util/ResourceBundle/results.json   0.0
javax/swing/JInternalFrame/results.json 0.0
org/w3c/dom/NamedNodeMap/results.json   0.0
java/lang/Runnable/results.json 0.0
javax/xml/parsers/SAXParserFactory/results.json 0.0
java/util/StringTokenizer/results.json  0.0
org/w3c/dom/ProcessingInstruction/results.json  0.0
javax/xml/transform/Transformer/results.json    0.0
javax/xml/namespace/QName/results.json  0.0
java/lang/ref/ReferenceQueue/results.json   0.0

答案 2 :(得分:1)

由于我的两个例子,小例子和完整的例子都与我的命令一起使用,我得出的结论是行结尾存在问题。使用diff和file命令证实了这种怀疑。由于某种原因,我有CRLF行结尾,并且awk不喜欢(真的吗?请支持所有类型的行结尾:()。

解决方案是:转换为LF行结尾。我使用了dos2unix。之后,这些东西按预期工作。

答案 3 :(得分:0)

> awk '$2~/^0.0$/' temp
java/util/Map$Entry/results.json    0.0
java/lang/NoClassDefFoundError/results.json 0.0
javax/xml/parsers/DocumentBuilderFactory/results.json   0.0
>

即使你的第二个命令也适合我。