Split /etc/passwd file using perl script

Last updated on November 17th, 2015 at 06:04 am

Click to rate this tutorial!
[Total: 7 Average: 3.3]

This tutorial will show you how to split /etc/passwd file using perl script.If you just have look at the passwd file you can see the lines are separated with semicolon “:” and that is being used to split the file here.

#!/usr/bin/perl
$filename = '/etc/passwd';
open(FILE, $filename) or die "Could not read from $filename, program halting.";
print "USERNAME                     USERID\n";
print "---------------------------------------\n";
while(<file>)
{
  chomp;
  @fields = split(':', $_);
  print "$fields[0]                   $fields[2]\n";

close FILE;
Click to rate this tutorial!
[Total: 7 Average: 3.3]

Leave a Reply

Your email address will not be published.