Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

“Invest in yourself — your confidence is always worth it.”

Explore Cosmetic Hospitals

Start your journey today — compare options in one place.

Ruby and Perl Scripts to List Perforce Clients in Descending Access Date Order

These scripts will:
✅ Connect to the Perforce server
✅ Retrieve a list of clients (workspaces)
✅ Sort them by last access date (descending order)


1. Prerequisites

Before running the scripts, ensure:
Perforce CLI (p4) is installed and accessible in PATH
✔ You have Perforce credentials (server, username)
✔ You can run p4 clients -l successfully in the command line


2. Ruby Script: List Perforce Clients by Last Access Date

#!/usr/bin/env ruby

# Define Perforce server details
P4PORT = "perforce.company.com:1666"
P4USER = "your_username"

# Run 'p4 clients -l' command and capture output
clients_output = `p4 -p #{P4PORT} -u #{P4USER} clients -l`

# Parse the output and extract client name and access date
clients = []
clients_output.split("\n\n").each do |entry|
  client = entry.match(/^Client (\S+)/)&.captures&.first
  access_time = entry.match(/^Access:\s+(\d+)/)&.captures&.first.to_i
  
  clients << { name: client, access: access_time } if client && access_time > 0
end

# Sort clients by last access date (descending order)
sorted_clients = clients.sort_by { |c| -c[:access] }

# Print results
puts "Perforce Clients Sorted by Last Access Date (Descending):"
sorted_clients.each do |client|
  puts "#{client[:name]} - Last Access: #{Time.at(client[:access])}"
end
Code language: PHP (php)

How to Run the Ruby Script

1️⃣ Save the script as list_p4_clients.rb
2️⃣ Replace "perforce.company.com:1666" and "your_username" with your actual Perforce server details
3️⃣ Open a terminal and run:

ruby list_p4_clients.rb
Code language: CSS (css)

3. Perl Script: List Perforce Clients by Last Access Date

#!/usr/bin/perl
use strict;
use warnings;

# Define Perforce server details
my $P4PORT = "perforce.company.com:1666";
my $P4USER = "your_username";

# Run 'p4 clients -l' command and capture output
my @clients_output = `p4 -p $P4PORT -u $P4USER clients -l`;

# Parse output to extract client name and access time
my @clients;
my ($client, $access_time);
foreach my $line (@clients_output) {
    if ($line =~ /^Client (\S+)/) {
        $client = $1;
    } elsif ($line =~ /^Access:\s+(\d+)/) {
        $access_time = $1;
        push @clients, { name => $client, access => $access_time } if $client && $access_time;
    }
}

# Sort clients by last access date (descending order)
@clients = sort { $b->{access} <=> $a->{access} } @clients;

# Print results
print "Perforce Clients Sorted by Last Access Date (Descending):\n";
foreach my $c (@clients) {
    my $date = scalar localtime($c->{access});
    print "$c->{name} - Last Access: $date\n";
}
Code language: PHP (php)

How to Run the Perl Script

1️⃣ Save the script as list_p4_clients.pl
2️⃣ Replace "perforce.company.com:1666" and "your_username" with your actual Perforce server details
3️⃣ Open a terminal and run:

perl list_p4_clients.pl
Code language: CSS (css)

4. Explanation

p4 clients -l retrieves all client workspaces with detailed info
✔ The script extracts the client name and last access date
✔ Clients are sorted in descending order based on last access timestamp
✔ Dates are converted from epoch time to human-readable format


5. Conclusion

This Ruby and Perl script allows you to efficiently list Perforce clients sorted by last access date.

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services — all in one place.

Explore Hospitals
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at <a href="https://www.cotocus.com/">Cotocus</a>. I share tech blog at <a href="https://www.devopsschool.com/">DevOps School</a>, travel stories at <a href="https://www.holidaylandmark.com/">Holiday Landmark</a>, stock market tips at <a href="https://www.stocksmantra.in/">Stocks Mantra</a>, health and fitness guidance at <a href="https://www.mymedicplus.com/">My Medic Plus</a>, product reviews at <a href="https://www.truereviewnow.com/">TrueReviewNow</a> , and SEO strategies at <a href="https://www.wizbrand.com/">Wizbrand.</a> Do you want to learn <a href="https://www.quantumuting.com/">Quantum Computing</a>? <strong>Please find my social handles as below;</strong> <a href="https://www.rajeshkumar.xyz/">Rajesh Kumar Personal Website</a> <a href="https://www.youtube.com/TheDevOpsSchool">Rajesh Kumar at YOUTUBE</a> <a href="https://www.instagram.com/rajeshkumarin">Rajesh Kumar at INSTAGRAM</a> <a href="https://x.com/RajeshKumarIn">Rajesh Kumar at X</a> <a href="https://www.facebook.com/RajeshKumarLog">Rajesh Kumar at FACEBOOK</a> <a href="https://www.linkedin.com/in/rajeshkumarin/">Rajesh Kumar at LINKEDIN</a> <a href="https://www.wizbrand.com/rajeshkumar">Rajesh Kumar at WIZBRAND</a> <a href="https://www.rajeshkumar.xyz/dailylogs">Rajesh Kumar DailyLogs</a>

Related Posts

Step by step guide on PerforceDFiles Tool | Perforce Tutorial

Step-by-Step Guide on PerforceDFiles Tool PerforceDFiles is a diagnostic tool used in Perforce Helix Core (P4) to list, analyze, and delete orphaned or corrupted files from the…

Read More

Ruby and Perl Script to find all Perforce users who have not set passwords

These scripts will:✅ Connect to the Perforce server✅ Retrieve a list of users✅ Identify users without passwords 1. Prerequisites Before running the scripts, ensure:✔ Perforce CLI (p4)…

Read More

The P4Win Window Navigation Guide in Perforce

P4Win Window Navigation Guide P4Win (Perforce for Windows) has a classic Windows-style interface, making it easy to navigate for developers familiar with GUI-based version control. This guide…

Read More

Complete Tutorial for P4Win (Perforce Visual Client)

Complete Tutorial for P4Win (Perforce Visual Client) Introduction P4Win is the Windows GUI client for Perforce, a powerful version control system (VCS) used for managing source code,…

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x