awk仅适用于复制的数据,为什么? [英] awk only works on copied data, why?

查看:60
本文介绍了awk仅适用于复制的数据,为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个稍微简单的awk,用于此处描述的目的:

I have a somewhat straightforward awk used for the purpose described here:

将多个标头信息字段追加到文件,直到找到下一个标头

Append multiple header information fields to file until next header found

awk仅在将数据复制/粘贴到新文件中后才可使用.例如,如果我将head的输出定向到一个新文件中,则awk仍然无法正常工作.仅当我将文件复制/粘贴到新文件时,awk才起作用.

The awk only works on the data after I copy/paste it into a new file. If I direct the output of head into a new file, the awk still does not work, for instance. The awk only works if I copy/paste the file into a new file.

`head -40 file.csv > output.csv`

这是awk:

`awk -F, '/"Serial No."/ {sn = $2} 
     /"Location:"/  {loc = $2} 
     /"([0-9]{1,2}\/){2}[0-9]{4} [0-9]{2}:[0-9]{2}"/ 
                    {$0 = loc FS sn FS $0}1' file.csv>master1.csv`

如果我复制/粘贴数据并将其与原始数据进行比较,则输出表明每一行都有差异,但没有说明在哪里.如果您看一下主要输出与复制/粘贴文件之间的区别,则会得到:

If I copy/paste the data and compare it to the original data, the output indicates a difference in every single line, but does not say where. If you look at a diff between a head output and a copy/paste files you get:

`diff trap4_top.csv trap4_again.csv'

:

 < 1,25c1,24

 < "Serial No.","0700000036022821"

 < "Location:","LS_trap_2c"
 < "High temperature limit (�C)",20
 < "Low temperature limit (�C)",0
 < "Date - Time","Temperature (�C)"
 < "5/28/2015 08:00",24.0
 < "5/28/2015 10:00",29.5
 < "5/28/2015 12:00",28.0
 < "5/28/2015 14:00",28.5
 < "5/28/2015 16:00",27.0
 < "5/28/2015 18:00",24.5
 < "5/28/2015 20:00",23.0
 < "5/28/2015 22:00",22.5
 < "5/29/2015 00:00",21.5
 < "5/29/2015 02:00",21.0
 < "5/29/2015 04:00",20.0
 < "5/29/2015 06:00",20.0
 < "5/29/2015 08:00",24.5
 < "5/29/2015 10:00",26.0
 < "5/29/2015 12:00",27.5
 < "5/29/2015 14:00",30.0
 < "5/29/2015 16:00",29.0
 < "5/29/2015 18:00",25.5
 < "5/29/2015 20:00",23.5
 < "5/29/2015 22:00",23.0
 ---
 > "Serial No.","0700000036022821"
 > "Location:","LS_trap_2c"
 > "High temperature limit (°C)",20
 > "Low temperature limit (°C)",0
 > "Date - Time","Temperature (°C)"
 > "5/28/2015 08:00",24.0
 > "5/28/2015 10:00",29.5
 > "5/28/2015 12:00",28.0
 > "5/28/2015 14:00",28.5
 > "5/28/2015 16:00",27.0
 > "5/28/2015 18:00",24.5
 > "5/28/2015 20:00",23.0
 > "5/28/2015 22:00",22.5
 > "5/29/2015 00:00",21.5
 > "5/29/2015 02:00",21.0
 > "5/29/2015 04:00",20.0
 > "5/29/2015 06:00",20.0
 > "5/29/2015 08:00",24.5
 > "5/29/2015 10:00",26.0
 > "5/29/2015 12:00",27.5
 > "5/29/2015 14:00",30.0
 > "5/29/2015 16:00",29.0
 > "5/29/2015 18:00",25.5
 > "5/29/2015 20:00",23.5`

我在diff中看到特殊字符,但是到目前为止,除了复制/粘贴以外,我是否参与其中,或者删除它们的确切方式不是.

I see special characters in the diff but I'm not if they're involved, or how exactly to remove them, other than copy/paste so far.

head trap4.csv | cat -vte

给予:

"Serial No.","0700000036022821"^M$
"Location:","LS_trap_2c"^M$
"High temperature limit (M-0C)",20^M$
"Low temperature limit (M-0C)",0^M$
"Date - Time","Temperature (M-0C)"^M$
"5/28/2015 08:00",24.0^M$
"5/28/2015 10:00",29.5^M$
"5/28/2015 12:00",28.0^M$
"5/28/2015 14:00",28.5^M$
"5/28/2015 16:00",27.0^M$

推荐答案

好的,我怀疑您的输入文件具有DOS行尾,即\r^M(如上所示).

Alright so as I suspected your input file has DOS line endings i.e. \r or ^M (as shown above).

您应该通过运行以下命令将输入​​文件转换为unix行结尾:

You should convert your input file to unix line endings by running:

dos2unix file.csv

否则,您可以执行以下操作:

Otherwise you can do:

head -40 file.csv | sed 's/\r//' | awk ...

这篇关于awk仅适用于复制的数据,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆