Posts Tagged With: Perl

Replacing in Found Files

Tagged with: , , , , , ,

Replace one string by another in all file found by find.


find . -name *whatyouwant* -exec perl -pi.bak -e 's/TEXT_TO_REPLACE/TEXT_TO_REPLACE_IT_WITH/g' {} \;
No Comments »

Perl Joke

Tagged with: , , , ,

Just a joke – no offense intended…


perl -e '$a="etbjxntqrdke";$a=~s/(.)/chr(ord($1)+1)/eg;print $a'
No Comments »

Install CPAN in Fedora

Tagged with: , , , , , ,

If you are getting a ‘Can’t locate CPAN.pm in @INC’ error while opening cpan, you have not installed Bundle::Cpan. You can do that without Cpan – using yum


yum install perl-CPAN
2 Comments »

Stop crond When mysqld is Down

Tagged with: , , , ,

A perl script to stop crond if mysqld is down


#!/usr/bin/perl

# See if MySQL server is alive or not
$mysql_status = `/etc/init.d/mysqld status`;
print "MySQL Status: $mysql_status\n";
unless($mysql_status =~ /running/) {
	print "Stopping Cron... ";
	#If MySQL is not running, kill the crond
	$cron = `/etc/init.d/crond stop`;
	print $cron;
}

print "\n";
No Comments »

git status Without the Untracked Files

Tagged with: , , , , ,

Don’t show the untracked files in git status


git status | perl -pe "exit if(/Untracked files\:/)"
5 Comments »

Sensex Figure using a Linux Command

Tagged with: , , , , , , ,

This one command will fetch and display the latest sensex figure in Linux. This command basically downloads a page, locates a specific line, and then extract just the necessary portion from it. I am sure you could find other uses for this method.


curl -s "http://money.rediff.com/money/jsp/markets_home.jsp" |grep ''|head -n1|perl -pe 's/^.+\">([\d\,\.]+)<\/TD>.+$/\1/i;'
No Comments »

Command to Print Until Regular Expression

Tagged with: , , , , ,

Print a file until a regular expression is matched.


cat file.txt | perl -pe "exit if(/Thats all/)"
No Comments »

Remove empty directories

Tagged with: , , , , , ,

Remove all empty directories within the current directory


find . -type d -empty -exec rmdir {} \;
perl -MFile::Find -e"finddepth(sub{rmdir},'.')"

Delete Empty Directories
http://www.jonasjohn.de/lab/red.htm
[tags]empty,folder,delete,remove,perl,command,linux[/tags]

No Comments »

Replace \n with another String

Tagged with: , , , ,

The command to convert all \n in a file to another string – very useful for list code generation.


perl -ne 's/\\n/\',\'/g;print;' file.txt>new.txt
1 Comment »

Remove Extension in Shell Scripting

Tagged with: , , , , , , ,

This command will list all the files a folder without their extensions. Comes handy when doing shell scripting


perl -e 'while(<*.*>){s/\..{3}$//i;print "File \"$_\"\n";}print "\n";'
No Comments »