From your comments it seems that your actual question is:
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.
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.
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!
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!"
)
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
and will print a record with n
fields separated by OFS
and ending with ORS
.
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
).
source: POSIX Awk
So now to answer the question. Your original line reads:
awk -v OFS=, '{ print $1 strftime(" %r")}' file1.jump > file2.csv
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
original (wrong) answer
I believe what you are after is a combination of:
So you could do it like,
awk '{ print $1,strftime(" %r")}' file | paste -s -d',' > file2
Or if it is just awk only,
awk '{ printf (NR==1?"":",") $1 OFS strftime(" %r") }' file > file2