Tip: Remove Duplicate Lines in File Using Perl
Here is little nifty code I found to snip or remove duplicate lines in a file using Perl. An example of a file that has duplicate lines looks like this:
/home/index.php
/home/links.php
/home/index.php
The result we want to achieve is this:
/home/index.php
/home/links.php
And here goes the code:
$ftmp = 'file.txt';
my %match = ();
{
local @ARGV = ($ftmp);
local $^I = '.tmp';
while(<>){
$match{$_}++;
next if $match{$_} > 1;
print;
}
}
The original file, file.txt, has its duplicate lines removed.
Tip: How to Use watch Command
I have never known that there is a command in Linux called watch. Basically, watch runs a program in a period of time, and displaying the output in real time.
watch command syntax is very simple. One typical use of the command watch is running it with traceroute. This is one example:
watch 'traceroute google.com'
This will run the traceroute command, display the output and when it is done, it repeats the process again.
Here a couple more examples of the watch command:
watch -n 10 free -m
This command will run the command free in ten-second intervals, and display the output.
watch -n 3 'cat /proc/interrupts'
This command will display the interrupts in 3-second intervals.
Search PinoyTux
Subscribe to Email Feeds
Blog Lounge
Popular Posts
Recent Posts
Drop your Card Here
Recent Comments
- smeaferrepove on Howto: Install yum On RHEL 4
- Anidich1 on Tip: Add User and Generate Password Script
- Tom S on Cebu Pacific Sucks
- kadersardar on PinoyTux Spreads Some CommentLuv
- Steve on Creative Labs Threatens Third Party Driver Modder








