awk Print help

Hi,

I got the output for the last command using the below command

last -w -F | awk '{print $1","$3","$5$6$7$8","$11$12$13$14","$15}' | tac | tr ',' '\t'

Now for the same output i want to add the below column names and then copy to csv file or xls file.

Can someone help me out here.

Column Names

USERNAME
HOSTNAME
LOGIN_TIME
LOGOUT_TIME
DURATION

Output looks like this

root    lt-j3rrf12.local.com May2009:25:042015       May2009:25:242015       (00:00)
root    lt-j3rrf12.local.com May2009:25:492015       May2009:25:512015       (00:00)
root    lt-defhkl32.local.com May2009:29:262015       May2019:40:292015       (10:11)
oracle  lt-defkl32.local.com May2009:31:402015       May2009:40:542015       (00:09)

Appreciate your help on this.

What OS are your on - some of your last optins are confusing.

Thanks for prompt response.

Am on Red Hat Linux

If you want to create a header with a known set of titles, can't you create an echo or a printf statement that will create that header?

If you want a CSV file instead of a tab delimited file, why are you changing the commas in the output from your awk command to tabs?

Can't you combine these two steps to get a script that produces the CSV output you want?

Don,

To be frank am very much new to the bash programming.Can you please help me with the code ?

Regards,

How about

last -w -F | tac | awk -vOFS=";" '
BEGIN   {print "USERNAME;HOSTNAME;LOGIN_TIME;LOGOUT_TIME;DURATION"
        }
        {print $1, $3, $5$6$7$8, $11$12$13$14, $15
        }
'

Rudy,

Thank you very much it helped.Can you please help me how to write this into a CSV or XLS file.

I tried this it is writing everything in one column.

last -w -F | tac | awk -vOFS=";" 'BEGIN   {print "USERNAME;HOSTNAME;LOGIN_TIME;LOGOUT_TIME;DURATION"}{print $1, $3, $5$6$7$8, $11$12$13$14, $15}' > /home/avaghar/Output.csv

USERNAME;HOSTNAME;LOGIN_TIME;LOGOUT_TIME;DURATION
wtmp;Tue;1915:49:472015;;
;;;;
reboot;boot;TueMay1915:50:13;TueMay1915:52:13;2015
root;:0;May1915:50:372015;May1915:51:152015;(00:00)
root;:0.0;May1915:50:462015;May1915:50:512015;(00:00)

Can you please help me how to split this and write data in each column.

Thanks,

That's exactly the structure of a .csv (comma separated values) file: just a block of text with fields and separators (commas, semicolons, ...) in between.
Just open that with EXCEL; it should offer sth. like a text wizard (or so).

1 Like

Rudy,

It is showing the same way as csv file,Below the output

USERNAME;HOSTNAME;LOGIN_TIME;LOGOUT_TIME;DURATION
wtmp;Tue;1915:49:472015;;
;;;;
reboot;boot;TueMay1915:50:13;TueMay1915:52:13;2015
root;:0;May1915:50:372015;May1915:51:152015;(00:00)
root;:0.0;May1915:50:462015;May1915:50:512015;(00:00)
reboot;boot;TueMay1915:56:39;MonJun1518:30:10;2015

Thanks again for your help.

The code you showed us in the 1st post in this thread produced tabular data aligned on tab stop boundaries (although the output you showed us replaced the tabs that your script produced with varying numbers of spaces that misaligned the output). You said you didn't want that; you said you wanted a CSV file output file format instead. You are getting exactly what you asked for. If you want a nicely formatted table style output with aligned output (even if the width of the text in the output columns varies), you can do that too; but nicely formatted table style output is MUCH harder to load into a MicroSoft Excel spreadsheet, while loading a CSV formatted file into MicroSoft Excel is easy.

1 Like