Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Everyday I will be given a new file which is exported from a remote production database. Now I need to replace a string with another string. Normally I open it up in vim & do the replacement manually. But since this file is 320 MB, what's a another way to do this without loading it up into memory with vim ?

P.S I am running on Ubuntu Lucid.

share|improve this question
1  
Essentially the same command in vim should be doable in sed. – MichaelT Feb 19 at 4:49
Using sed seems appropriate here. Also, if you know that the pattern appears only once, and it appears near end of the file, then you may also want to look at this link – agent13 Feb 19 at 5:19
Depending on how you get the file & what you're doing with it, there may be better options. If you're pulling it through FTP or HTTP, you could have curl stream output through a pipe to sed rather than downloading & modifying it in the filesystem. If you're using some sort of script to put it into another database, you could massage your data there. – Sean McSomething Feb 19 at 17:12

closed as off topic by Martijn Pieters, MainMa, Glenn Nelson, Walter, GlenH7 Feb 19 at 14:47

Questions on Programmers Stack Exchange are expected to relate to software development within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

2 Answers

up vote 6 down vote accepted

Build a bash script that uses sed to find the offending string, mutate it to something desirable and replace it within the file. There are many tutorials on how to do this.

If you are for some reason using Vim in Windows (yes it's a thing), then you can do it with cat as this tutorial demonstrates.

share|improve this answer
You can even cron it if it's pretty much the same text. – Manatax Feb 19 at 4:55
sed in a bash script is exactly what I need. – Bon Ami Feb 19 at 6:11
Ah, sometimes the old ways are still the best! – Peter Rowell Feb 19 at 6:54

sed or AWK are probably what you are looking for. The Advanced Bash Scripting Guide at the Linux Documentation Project has a short introduction to both sed and AWK.

sed is pretty straightforward for simple uses though:

sed -e 's/oldtext/newtext/g' -i.bak file

The -e flag shows the expression follows directly. The -i flag tells it to change the file in-place, but to make a copy of the original called file.bak.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.