awk Print help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk Print help
# 1  
Old 10-01-2015
awk Print help

Hi,

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

Code:
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

Code:
USERNAME
HOSTNAME
LOGIN_TIME
LOGOUT_TIME
DURATION

Output looks like this

Code:
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.

Last edited by Don Cragun; 10-01-2015 at 10:40 PM.. Reason: Add CODE tags.
# 2  
Old 10-01-2015
What OS are your on - some of your last optins are confusing.
# 3  
Old 10-01-2015
Thanks for prompt response.

Am on Red Hat Linux
# 4  
Old 10-01-2015
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?
# 5  
Old 10-02-2015
Don,

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

Regards,
# 6  
Old 10-02-2015
How about
Code:
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
        }
'

# 7  
Old 10-02-2015
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.

Code:
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,

Last edited by Don Cragun; 10-02-2015 at 05:22 PM.. Reason: Add CODE tags.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: Print count for column in a file using awk

Hi, I have the following input in a file & need output as mentioned below(need counter of every occurance of field which is to be increased by 1). Input: 919143110065 919143110065 919143110052 918648846132 919143110012 918648873782 919143110152 919143110152 919143110152... (2 Replies)
Discussion started by: siramitsharma
2 Replies

2. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

3. Shell Programming and Scripting

AWK print AWK command

awk '{print "awk '{sub(/pdb_00/,"pdb_"$0"_00"); print}' pdb_"$0"_00.namd > tempo"; print "mv tempo pdb_"$0"_00.namd"}' datA2.dat > copy_script2.bash This works when trying to print 'sed etc. etc' but if I switch to using AWK to print a set of AWK commands it doesn't work... e.g. this... (3 Replies)
Discussion started by: chrisjorg
3 Replies

4. UNIX for Advanced & Expert Users

awk print help

In this: $ awk '{ print "var is", var }' var=TEST file1 What does the print part do? { print "var is", var } (4 Replies)
Discussion started by: cokedude
4 Replies

5. Shell Programming and Scripting

Print using awk

Hi.. I have two files. contents of the files are as below. header1.dat ------------- This is a header record. header2.dat ------------- This is a header record. Date:2011-01-05 I am executing the below scripts. HEADER1=`cat header1.dat` HEADER2=`cat header2.dat` awk 'BEGIN... (2 Replies)
Discussion started by: 46019
2 Replies

6. Shell Programming and Scripting

print through AWK

Hi friends I need one help from you . I want to print name='JACK' through . how can i make it . awk '{print "name=jack"}' it ll print name=JACK (6 Replies)
Discussion started by: imipsita.rath
6 Replies

7. Shell Programming and Scripting

Awk problem: How to express the single quote(') by using awk print function

Actually I got a list of file end with *.txt I want to use the same command apply to all the *.txt Thus I try to find out the fastest way to write those same command in a script and then want to let them run automatics. For example: I got the file below: file1.txt file2.txt file3.txt... (4 Replies)
Discussion started by: patrick87
4 Replies

8. Shell Programming and Scripting

Awk and print help

Hello I have a log file like so 2009-01-15 17:55:06 H=host-216-153-217-114.spr.choiceone.net :30675 I=:25 F=<> rejected RCPT <feast@test.co.uk>: DNSBL listed at sbl-xbl.spamhaus.org What I am trying to do is be able to print x amount of columns after a certain part of the line say F= grep... (1 Reply)
Discussion started by: casseyjon
1 Replies

9. Shell Programming and Scripting

How do I get awk to print a " in it's print part?

The line is simple, use " '{ print $1"]"$2"\"$3THE " NEEDS TO GO HERE$4 }' I've tried \", "\, ^" and '"" but none of it works. What am I missing? Putting in the [ between $1 and $2 works fine, I just need to do the same with a ". Thanks. (2 Replies)
Discussion started by: LordJezo
2 Replies

10. UNIX for Dummies Questions & Answers

awk print $2

/oracle/ID1/saparch"|awk '{print $2}' what does print $2 means? (1 Reply)
Discussion started by: yls177
1 Replies
Login or Register to Ask a Question