1

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

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

4 回答 4

4

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

除此之外:

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

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

于 2013-09-26T20:02:39.657 回答
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
于 2013-09-26T10:21:12.700 回答
1

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

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

于 2013-09-26T12:02:06.753 回答
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
>

甚至您的第二个命令也对我有用。

于 2013-09-26T10:20:46.533 回答