Amend format of output from "last"command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Amend format of output from "last"command
# 1  
Old 12-16-2010
Amend format of output from "last"command

When I run a “last” command, I am getting more than one row per user.

Eg
Code:
userabc pts/2 10.253.0.108 Tue Dec 14 09:21 still logged in
userabc pts/2 10.253.0.108 Tue Dec 14 03:57 - 03:59 (00:01)
userabc pts/2 10.253.0.108 Mon Dec 13 14:25 - 15:43 (01:18)
 
userpqr pts/3 10.253.0.39 Wed Jan 13 15:26 - 18:53 (03:26)
userpqr pts/3 10.253.0.39 Wed Jan 13 15:20 - 15:21 (00:00)
 
userxyz pts/6 10.1.0.23 Thu Jan 14 14:14 still logged in
userxyz pts/6 10.1.0.23 Thu Jan 14 13:40 - 13:42 (00:01)

I need to transfer this file to an external application,
and then import its contents to populate a new table.

Before doing so, I need to remove duplicates, leaving only the most recent entry,
I also need to insert the “|” character between the fields, and remove all spaces, as the external application demands this convention for its import to work.

So the above would end up looking like :
Code:
userabc|pts/2|10.253.0.108|Tue|Dec|14|09:21|still|logged|in|
userpqr|pts/3|10.253.0.39|Wed|Jan|13|15:26|-|18:53|(03:26)|
userxyz|pts/6|10.1.0.23|Thu|Jan|14|14:14|still|logged|in|

Is this possible, within a single unix script ?

Many thanks in anticipation.

Moderator's Comments:
Mod Comment Please use code tags - you got a PM how to do that.

Last edited by zaxxon; 12-16-2010 at 10:55 AM.. Reason: code tags
# 2  
Old 12-16-2010
This as a start:
Code:
$> awk '$1==u || /^$/ {next} {print; u=$1}' infile
userabc pts/2 10.253.0.108 Tue Dec 14 09:21 still logged in
userpqr pts/3 10.253.0.39 Wed Jan 13 15:26 - 18:53 (03:26)
userxyz pts/6 10.1.0.23 Thu Jan 14 14:14 still logged in

Having something like:
Code:
...|still|logged|in

will not be very helpful if you tend to import it into a database table for example. There would be some replacing of the blanks between the date in such cases be involved. I have no good short idea to do this currently but I guess someone will have one ready Smilie
# 3  
Old 12-16-2010
Thanks zaxxon for your prompt responses.
I have run your code, and it is certainly removing all the duplicates.
But I am still not getting a "|" command instead of the spaces separating the fields.
Any further advice would be most welcome.
Thanks again !
# 4  
Old 12-16-2010
Code:
awk 'NF && !a[$1]++{NF=NF+1;print}' OFS="|" file

# 5  
Old 12-17-2010
Thanks Franklin52.
Unfortunately, I am getting :- syntax error near line 1
bailing out near line 1

I created a script containing your code as follows :-
awk 'NF && !a[$1]++{NF=NF+1;print}' OFS="|" myfile
where myfile is the sorted output from the 'last' command.
Am I meant to set NF to a new file name ?
Or set [$1] to a runtime parameter ?
If so, what value ?
Sorry if any of this is obvious, but I am not an expert.
Thanks again for your help.

---------- Post updated at 12:41 PM ---------- Previous update was at 09:23 AM ----------

Hi

I have worked out that I can use

Code:
 

    tr –s  ‘[:space:]’


to reduce the spaces to a single space, then
Code:
 

    sed “s/ /\|/g


to change the single space into a |.

Is there an easier method ?
# 6  
Old 12-17-2010
Quote:
Originally Posted by malts18
Thanks Franklin52.
Unfortunately, I am getting :- syntax error near line 1
bailing out near line 1
Use nawk or /usr/xpg4/bin/awk on Solaris.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Solaris 10 Sparc. How to change Vendor info of SAN disks reported in "format" command?

Greetings! After block level migration using an external appliance, the luns are getting reported as DGC-RAID5 and these luns are infact from the new storage. I have a query on changing the device Vendor info from DGC-RAID5 to HP3par in the format o/p only. AVAILABLE DISK SELECTIONS: ... (3 Replies)
Discussion started by: n_Bhaskar
3 Replies

2. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

3. Shell Programming and Scripting

"links -dump" output format issue

Hi All, I tried searching a lot about this but to no avail. I have a HTML file. I used links -dump file_page.html > text_html.txt What the above command gave me was a filtered text from the HTML file with tags removed. Now, the the output from the above command looked something like this:... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. Shell Programming and Scripting

Format output from the file to extract "date" section

Hello Team , I have to extract date section from the below file output. The output of the file is as shown below. I have to extract the "" this section from the above output of the file. can anyone please let me know how can we acheive this? (4 Replies)
Discussion started by: coolguyamy
4 Replies

6. Shell Programming and Scripting

Can someone help me to convert the date format after get it from the "LAST REBOOT" command.

Can someone help me to convert the date format after get it from the "LAST REBOOT" command. these is the standard output. bash-3.00# last reboot reboot system boot Fri Aug 6 15:07 reboot system down Fri Aug 6 15:04 reboot system boot ... (3 Replies)
Discussion started by: pichitw
3 Replies

7. UNIX for Dummies Questions & Answers

Format output from "echo" command

Hi, I have written a BASH shell script that contains a lot of "echo" commands to notify the user about what's going on. The script generates a log file that contains a copy of what is seen in the terminal. The echo statements are generally verbose, and thus extend out for quite a ways on one... (2 Replies)
Discussion started by: msb65
2 Replies

8. Shell Programming and Scripting

"Join" or "Merge" more than 2 files into single output based on common key (column)

Hi All, I have working (Perl) code to combine 2 input files into a single output file using the join function that works to a point, but has the following limitations: 1. I am restrained to 2 input files only. 2. Only the "matched" fields are written out to the "matched" output file and... (1 Reply)
Discussion started by: Katabatic
1 Replies

9. UNIX for Dummies Questions & Answers

Explanation of "total" field in "ls -l" command output

When I do a listing in one particular directory (ls -al) I get: total 43456 drwxrwxrwx 2 root root 4096 drwxrwxrwx 3 root root 4096 -rwxrwxr-x 1 nobody nobody 3701594 -rwxrwxr-x 1 nobody nobody 3108510 -rwxrwxr-x 1 nobody nobody 3070580 -rwxrwxr-x 1 nobody nobody 3099733 -rwxrwxr-x 1... (1 Reply)
Discussion started by: proactiveaditya
1 Replies

10. Debian

Debian: doubt in "top" %CPU and "sar" output

Hi All, I am running my application on a dual cpu debian linux 3.0 (2.4.19 kernel). For my application: <sar -U ALL> CPU %user %nice %system %idle ... 10:58:04 0 153.10 0.00 38.76 0.00 10:58:04 1 3.88 0.00 4.26 ... (0 Replies)
Discussion started by: jaduks
0 Replies
Login or Register to Ask a Question