| |||||||||||
| |||||||||||
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 ![]() ![]() 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:
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.
|
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |