Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
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.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2022: 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