Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Perl can be used for many things, including website CGI stuff like PhP (connecting to databases, sending mails, etc - all via specific "modules"). But it was primarily designed for system admins and text manipulation.
Check out: http://www.cpan.org/ for starters.
The different Perl man pages ("man perlsyn" for syntax , "man perlfun" for built-in functions, "man perldata" for data structures) can help you along as well. "man perl" gives an overview of all Perl man pages.
As for the script I showed you:
Code:
#Standard reference to interpreter (like for normal shell scripts):
#!/usr/local/bin/perl
#if you don't want to use STDIN, put an "open()" call here.
#<> reads a line from STDIN and puts it in the special $_ variable.
#The while loop simply iterates over all lines at STDIN.
while(<>)
{
#The current line from STDIN is accessible via $_;
#chop() is equal to chop($_) and chops off the last character from the string in $_
#Hence, the "\n" (newline) at the end of each line get eliminated.
chop();
#This is short, regular expression syntax.
#Basically, the if-test determines if $_ starts with (^) the string "insert into" and the "i" option makes the match
#case insensitive.
if (/^insert into/i)
{
#Here, we split the current line into pieces, based on the space as delimiter.
#The result doesn't get stored in an array (or associative array/hash), but in separate variables via
#the () list operator.
#So, the first word (everything up to the first space) gets assigned to "undef", the second as well, the third
#to $table, etc.
#You can think of undef as a special variable to which you can assign stuff you don't want to store.
#As a result, we'll only store the 3rd and 5th words of each INSERT INTO statement in $table and $records.
(undef,undef,$table,undef,$records)=split(/ /, $_);
#$records contains a string like (val1,val2,val3),(val4,val5,val6),... (see an example mysqldump file)
#So, the inserted records are separated by "," and I use split() once more to separate them.
#This time, all records (since I don't know how many there are), are assigned to an array, called @recArray.
#(just like '$' denotes a normal, scalar variable, '@' indicates an array and '%' a hash array).
@recArray=split( /\,/ , $records);
#prints table name
print $table;
#Loop over each record in the @recArray array.
#In each iteration, the current record in the array is stored in $rec.
foreach $rec @recArray
{
#$rec now has format (val1,val2,val3,...) => omit the '(' and ')' to get csv
#If you know sed, the next syntax shouldn't be to awkward.
#s/()// just substitutes '(' and ')' by nothing. The "g" option makes the substitution global,
#so all occurrences of '(' and ')' get substituted.
#=~ operator means "apply the substition to $rec and assign the result in $rec afterwards".
$rec=~s/()//g;
#This prints the result.
print $rec;
#In fact, coming to think of it,
print "$rec\n";
#is probably a better idea, since that will put the csv records on separate lines...
}
}
}
Wow timekke that was some awesome explanation and thxs aton for the links. It was wonderful. The mysqldump has been taken care of for the client. Cant express my thxs enough buddy. You have really been of great help. Now I know whom to look upto in case of any doubts. My email address is rajnair0278@yahoo.com just incase you are online. Thx again. Cheers!!!.
You're very welcome, rajnair0278.
I'm just happy we got it sorted out together. That's what these forums are for.
Quote:
Now I know whom to look upto in case of any doubts.
Well, that's a very kind compliment. But in all modesty, I need to say that there are far better Linux users than I am on these forums.
Just a hint: if you don't like to get tons of spam, I suggest that you never post your e-mail address like that on a forum. When I started using a Microsoft forum for the first time, I made the same mistake and I still get spam e-mail because of it (even though the mistake was made over a year ago). Thank god for spam filters...
I've added you to my buddy list (click on my username and you can do the same).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.