代码之家  ›  专栏  ›  技术社区  ›  kutschkem

awk与零比较失败?

awk
  •  1
  • kutschkem  · 技术社区  · 11 年前

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

    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 回复  |  直到 7 年前
        1
  •  4
  •   Ed Morton    11 年前

    您的输入文件是在Windows上创建的,因此每行末尾都有虚假的控件Ms。您已经发现可以在输入文件上运行dos2unix来解决这个问题。

    除此之外:

    Awk不适用于线路,它适用于记录。awk的默认记录分隔符(RS)是一个换行符,所以它在外行看来就像awk在行上工作一样,但这只是一种默认行为。你可以告诉awk使用其他字符来分隔记录,但对于大多数awk来说,RS只能是一个字符,所以当微软决定插入一个2个字符的行尾字符串时 <CR><LF> 那么这就产生了一个问题。

    使用GNU awk(可能还有其他一些),您可以为RS指定一个多字符正则表达式,这样他们只需将RS设置为 <CR>&书信电报;低频> 但对于其他awk,您必须将Microsoft的多字符行分隔符字符串转换为awks RS可以设置的单字符记录分隔符,例如,通过运行dos2unix来去掉控件Ms,只保留换行符,以便awks默认RS按原样处理。

        2
  •  1
  •   kutschkem    11 年前

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

    解决方案是:转换为LF行结尾。为此,我使用了dos2unix。在那之后,这些东西就如预期的那样工作了。

        3
  •  1
  •   Kent    11 年前

    试试这一行:

    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
    
        4
  •  0
  •   Vijay    11 年前
    > 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
    >
    

    甚至你的第二个命令也对我有效。