Training, Open Source computer languages

This is page http://www.wellho.net/forum/Perl-Programming/replace- ... -file.html

Our email: info@wellho.net • Phone: 01144 1225 708225

 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
replace text in a large text file

Posted by John_Moylan (jfp), 14 November 2002
Problem:
I had to move a large (800Mb) mysqldump to port the db over to sql server.
Unfortunatly the mysqldump put the string 'TYPE=MyISAM' at the end of each table schema.
Who wants to open a 800Mb text file and do a search and replace? offers?

Anyway, Perl to the rescue.
Solution:
perl -p -i.bak -e 's/pattern1/pattern2/g' MysqlDump.sql
(On the command line)
pattern1 is the search string
pattern2 is the replacement
(Well I'm stating the obvious I know as its just doing a  s///)

The long and the short of it is that this modified my 800Mb text file in place, saved an original .bak version, all on the command line in about 5-10 minutes.

Perl can be a damned life saver.

jfp


Posted by kazaguri (Kazaguri), 17 February 2003
Hi,

That's a good solution to that problem.  Some people wouldn't have perl though.

A couple of solutions in shell :

cat MysqlDump.sql | sed -e 's/pattern1/pattern2/g' > MysqlDump.sql.temp
mv MysqlDump.sql.temp MysqlDump.sql

grep -v pattern1 MysqlDump.sql > MysqlDump.sql.temp
mv MysqlDump.sql.temp MysqlDump.sql



Posted by admin (Graham Ellis), 17 February 2003
Two good solutions.  The beauty of Open Source is that there's always lots of ways of doing it    .   The perl solution might be slightly slower to run that the sed one  ... but then it will work without any problem on operating systems that don't have cat grep or mv commands!

By the way - have you come across s2p that lets you convert a sed script into a perl one?

Welcome, Kazaguri ... thanks for joining and posting!

Posted by enquirer (enquirer), 17 June 2005
I've received the following message to follow up this thread ... I'm posting it here to add an interesting different view.  It's a very long time since I wrote my original, but I think I was celebrating the fact that open source products provides a lot of extra ways of doing something, and I wasn't saying that it couldn't be done with Microsoft products.  Perhaps I didn't make that quite clear.

By the way, I do agree that a beauty of open source is at my correspondent say - but, you know, that's not the ONLY beauty of it; to have just that one selling feature would leave it pretty thin!

- Graham

Quote:
The comment in question is: "The beauty of Open Source is that there's always lots of ways of doing it"

writing an algorithm in perl or a unix shell script has absolutely NOTHING (at all) to do with open source anything.  I could re-write these two scripts in VBscript and again in windows shell script - neither of which is open source.  In addition, the beauty of open source is not that there are a lot of ways to do it.  It's that anyone has the freedom to view the code and make enhancements and fix defects.

please don't respond to this message.



Posted by emsti (emsti), 6 July 2005
Hi,

since this is to be run off the command lines I was wondering if I could have pattern1 containing spaces {path delimiters etc..} in there?

would I just use escape sequences?

Also how would that work for the 'sed ' option?

I was wondering if you could refer me to an online reference with regex examples, file handling and basically scripting for perl (and sed). There are too many out there  

Posted by admin (Graham Ellis), 7 July 2005
If you want to include patterns that include spaces on the command line, it's not a problem; you simply need to ensure that they're part of a string that's protected from the shell - probably as part of a quoted string (I THINK all the example above do that but I'm on a slow hotel-line connection today so just dashing off a brief reply).



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho