Thursday, August 14, 2008

Reversing a file

You have a file and you wanted to reverse it. How to do it? Guys...... its really very easy.. Use below commands:


tail -r filename
or
cat -n filename | sort -nr | cut -c8-

You can achieve the same by writing a perl script. See below perl script:


#!/usr/bin/perl

$filename = "a.java";
open(INFILE, "<", $filename)
or die "Can't open $filename for reading: $!\n";
my(@content) = <INFILE>;
close(INFILE);
while (@content) {
my($line) = pop(@content);
chomp $line;
print "$line \n";
}

No comments: