Awk将两个字段输出到CSV文件中,但是CSV文件将两个字段都放在同一单元格中 [英] Awk outputs two fields into a CSV file, but CSV file places both fields in the same cell

查看:229
本文介绍了Awk将两个字段输出到CSV文件中,但是CSV文件将两个字段都放在同一单元格中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是对 Awk的跟进问题: 当前时间"输出为零

我正在尝试将file1中的第一个字段和当前时间(作为两个单独的字段)发送到.CSV文件中,以便field1将填充与field2不同的单元格.

I am trying to send the first field in file1 and the current time, as two seperate fields, into a .CSV file so that field1 will populate a different cell than field2.

例如在文件1中.一行看起来像这样:

E.g.in file one. a row looks like this:

IN-X_4096_20140802_121306_007 `random text`

[edit:]其中在file1的这一特定行中,007结束了第一个字段,随机文本"表示该行中出现了许多字段以及上述的第一个字段.

[edit:] where in this particular row of file1, the 007 ends the first field, and 'random text' means numerous fields appear in the row along with the first field described above.

[edit:]我希望字段1取自file1,并成为file2.CSV中的单元格.因此,file2.csv中的第1列将始终是文件1中的字段1.棘手的部分似乎是使file2.csv中的第2列包含时间.

[edit:] I want that field 1 is taken from file1 and becomes a cell in the file2.CSV. So column 1 in file2.csv will always be field 1 from file 1. The tricky part seems to be to make column 2 in file2.csv contain the time.

N-X_4096_20140802_121306_007 (cell in column A) 14:24:32 (cell in column B)

我正在使用的代码是:

awk -v OFS=, '{ print $1 strftime(" %r")}' file1.jump > file2.csv

此刻,此代码导致.CSV文件将整个输出放置在file2.csv中每一行的一个单元格中.有没有办法使输出进入.CSV中的单独单元格?

At the moment, this code results in the .CSV file placing the entire output into one cell, for each row, in file2.csv. Is there a way to make the output go into seperate cells in the .CSV?

推荐答案

从您的评论看来,您的实际问题是:

From your comments it seems that your actual question is:

print语句如何工作以及如何受到变量OFS的影响.

How does the print statement work and how is it affected by the variable OFS.

首先,重要的是要认识到记录字段.

First of all, it is important that realize that the two most important concepts to awk are records and fields.

通过 record 读取通过各种方式(stdingetline)馈入awk的输入 record ,其中每个记录由记录分隔符分隔由RS定义.由于默认情况下RS是< newline>字符\n,实际上是一条记录,因此awk会默认处理一行一行的文件.

The input which is fed into awk through various means (stdin or getline) is read record by record where each record is separated by a record separator which is defined by RS. Since RS is by default the <newline> character \n, a record is actually a line and thus awk processes default a file line-by-line.

当读取一条记录/行时,awk会将记录拆分为多个字段,其中每个字段都由字段分隔符FS(可以是正则表达式)分隔.默认情况下,字段分隔符FS设置为任何< blank>序列.人物.这意味着,默认情况下,每个字段都是一个单词.如果重新定义FS,则字段将不同.例如

When a record/line is read, awk will split the record in fields where each field is separated by the field separator FS (which can be a regular expression). By default, the field separator FS is set to be any sequence of <blank> characters. Which means that, by default, each field is a word. If you redefine FS, fields will be different. Eg.

Mooo, that sexy cow!

默认情况下具有4个字段($1="Mooo,"$2="that""$3="sexy"$4="cow!"),但是如果FS=","则只有2个字段($1="Mooo"$2=" that sexy cow!")

has 4 fields by default ($1="Mooo,", $2="that", "$3="sexy" and $4="cow!") but if FS="," it only has 2 fields ($1="Mooo" and $2=" that sexy cow!")

以上内容不仅是关于输入以及awk如何理解它的信息,而且在输出中还知道了 records fields 的概念.这就是print语句的来源.print语句使您可以打印由各种字段构成的记录.输出记录分隔符ORS,默认情况下为< newline>.字符\n,告诉您如何分隔两条记录和输出字段分隔符OFS,默认情况下为< space>. ,告诉您如何分隔字段.打印语句看起来像

The above is all about input and how awk understands it, but also in output the concept of records and fields is known. And this is where the print statement comes in. The print statement allows you to print a record which is build of various fields. The output record separator ORS, by default a <newline> character \n, tells you how two records are separated and the output field separator OFS, by default a <space> , tells you how the fields are separated. The print statement looks like

print arg1, arg2, ..., argn

并打印一条记录,其中记录中的n字段用OFS分隔,并以ORS结尾.

and will print a record with n fields separated by OFS and ending with ORS.

print语句应将每个表达式自变量的值写入由当前输出字段分隔符分隔的指示输出流(请参见上面的变量OFS),并由输出记录分隔符终止(请参见上面的变量ORS) .所有表达式自变量均应视为字符串,并在必要时进行转换;此转换应如awk中的表达式中所述,但要使用OFMT中的printf格式代替CONVFMT中的值.空表达式列表应代表整个输入记录($0).

The print statement shall write the value of each expression argument onto the indicated output stream separated by the current output field separator (see variable OFS above), and terminated by the output record separator (see variable ORS above). All expression arguments shall be taken as strings, being converted if necessary; this conversion shall be as described in Expressions in awk, with the exception that the printf format in OFMT shall be used instead of the value in CONVFMT. An empty expression list shall stand for the whole input record ($0).

源: POSIX Awk

所以现在回答这个问题.您的原始行显示为:

So now to answer the question. Your original line reads:

awk -v OFS=, '{ print $1 strftime(" %r")}' file1.jump > file2.csv

此处OFS无效,因为print仅具有一个读取$1 strftime(" %r")的参数(请注意,$1strftime之间的空格没有意义,可以忽略,因此两个字符串都串联在一起.)你想要的是这个

here OFS has no effect as print has only a single argument which reads $1 strftime(" %r") (note that the space between $1 and strftime has no meaning and can be ignored, so both strings are concatenated. So what you want is this:

awk -v OFS=, '{ print $1, strftime("%r")}' file1.jump > file2.csv


原始(错误)答案

我相信您追求的是以下各项的组合:

I believe what you are after is a combination of:

  • "Current time" outputs as zero
  • use awk to print a column, adding a comma

所以您可以这样做

awk '{ print $1,strftime(" %r")}' file | paste -s -d',' > file2

或者如果仅仅是awk,

Or if it is just awk only,

awk '{ printf (NR==1?"":",") $1 OFS strftime(" %r") }' file > file2

这篇关于Awk将两个字段输出到CSV文件中,但是CSV文件将两个字段都放在同一单元格中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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