IBM Support

AIX Commands: How to use Perl to parse .sh_history

How To


Summary

The fc command only reports the history for my user. How can I parse another .sh_history file?

Steps

1) Create a perl script to parse the history file and present the formatted date.

Note: Some history file lines might contain characters that produce unexpected results, you will need to modify regex calls as needed.

# cat /tmp/myscript.pl

#!/usr/bin/perl
use POSIX qw(strftime);
my $filename = shift @ARGV;
open(my $fh, "<", $filename) or die "Failed to open file: $filename - $!";
while (my $string = <$fh>) {
    chomp $string;
    my ($timestamp) = $string =~ /#(\d+)#/;
    if ($timestamp) {
        my $formatted_time = strftime("%Y-%m-%d %H:%M:%S", localtime($timestamp));
        print "Line: $string\n";
        print "Timestamp (Epoch): $timestamp\n";
        print "Timestamp (Formatted): $formatted_time\n";
    }
}
close($fh);


Run the script with the file as an argument.

# /tmp/myscript.pl /.sh_history
Line: lslpp -l #▒#1686075298#▒#
Timestamp (Epoch): 1686075298
Timestamp (Formatted): 2023-06-06 19:14:58
Line: ps -ef #▒#1686075301#▒#
Timestamp (Epoch): 1686075301
Timestamp (Formatted): 2023-06-06 19:15:01
Line:  /tmp/p1 /.sh_history #▒#1686075307#▒#
Timestamp (Epoch): 1686075307
Timestamp (Formatted): 2023-06-06 19:15:07
Line: mv /tmp/p1 /tmp/myscript.pl #▒#1686075329#▒#
Timestamp (Epoch): 1686075329
Timestamp (Formatted): 2023-06-06 19:15:29
Line: /tmp/myscript.pl /.sh_history #▒#1686075338#▒#
Timestamp (Epoch): 1686075338
Timestamp (Formatted): 2023-06-06 19:15:38

 




Additional Information

SUPPORT

If you require more assistance, use the following step-by-step instructions to contact IBM to open a case for software with an active and valid support contract.  

1. Document (or collect screen captures of) all symptoms, errors, and messages related to your issue.

2. Capture any logs or data relevant to the situation.

3. Contact IBM to open a case:

   -For electronic support, see the IBM Support Community:
     https://www.ibm.com/mysupport
   -If you require telephone support, see the web page:
      https://www.ibm.com/planetwide/

4. Provide a clear, concise description of the issue.

 - For more information, see: Working with IBM AIX Support: Describing the problem.

5. If the system is accessible, collect a system snap, and upload all of the details and data for your case.

 - For more information, see: Working with IBM AIX Support: Collecting snap data


[{"Type":"MASTER","Line of Business":{"code":"LOB08","label":"Cognitive Systems"},"Business Unit":{"code":"BU058","label":"IBM Infrastructure w\/TPS"},"Product":{"code":"SWG10","label":"AIX"},"ARM Category":[{"code":"a8m0z000000cvzgAAA","label":"Commands"}],"ARM Case Number":"","Platform":[{"code":"PF002","label":"AIX"}],"Version":"All Versions"}]

Document Information

More support for:
AIX

Component:
Commands

Software version:
All Versions

Operating system(s):
AIX

Document number:
7001741

Modified date:
06 June 2023

UID

ibm17001741

Manage My Notification Subscriptions