Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux command to replace string in HUGE file with another string

Tags:

vim

sed

I have a huge file (8GB), I want replace on the first 30 lines the String LATIN1 with UTF-8 what is the most efficient method? Means exist there a way to use probably sed but to quit after parsed first 30 lines.

VIM was not able to save the file in 3 hours.

like image 978
Nenad Avatar asked Mar 12 '26 22:03

Nenad


1 Answers

The problem is that in the event of a replacement, all programs will make a copy of the file with the substitution in place in order to replace the original file ultimately -- they don't want to risk losing the original for obvious reasons.

With perl, you can do this in a one-liner, but that doesn't make it any shorter (well, it probably does compared to vim, since vim preserves history in yet another file, which perl doesn't):

perl -pi -e 's,\bLATIN1\b,UTF-8,g if $. <= 30' thefile
like image 193
fge Avatar answered Mar 15 '26 16:03

fge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!