LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 06-08-2006, 06:51 AM   #16
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61

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...
    }
  }
}
 
Old 06-09-2006, 04:38 AM   #17
rajnair0278
Member
 
Registered: Mar 2006
Posts: 62

Original Poster
Rep: Reputation: 15
Smile

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!!!.
 
Old 06-09-2006, 04:46 AM   #18
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
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).

Cheers to you too!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
mysqldump stops marciano Linux - Software 2 04-29-2006 08:23 PM
mysqldump questions wh33t Programming 1 10-24-2005 08:07 PM
about mysqldump javier_ccs Programming 3 08-08-2005 06:13 PM
mysqldump - backup *.* bpk Linux - Newbie 1 03-29-2004 04:42 PM
MySQLDump and Cron duerra Linux - Newbie 6 02-20-2004 08:18 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration