Linux命令将LARGE文件中的字符串替换为另一个字符串 [英] Linux command to replace string in LARGE file with another string

查看:241
本文介绍了Linux命令将LARGE文件中的字符串替换为另一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个巨大的SQL文件在服务器上执行。转储是从我的机器,在它有一些与我的机器相关的设置。所以基本上,我想要每次出现c:// temp以替换// home // some // blah / code>

I have a huge SQL file that gets executed on the server. The dump is from my machine and in it there are a few settings relating to my machine. So basically, I want every occurance of "c://temp" to be replace by "//home//some//blah"

如何从命令行执行此操作?

How can this be done from the command line?

推荐答案

sed 是大型文件的不错选择。

sed is a good choice for large files.

sed -i.bak -e 's%C://temp%//home//some//blah%' large_file.sql

这是一个不错的选择,因为不能立即读取整个文件它。引用手册:

It is a good choice because doesn't read the whole file at once to change it. Quoting the manual:


流编辑器用于对输入执行
基本文本转换
流文件或来自
管道的输入)。虽然在某些方面类似
到编辑器允许脚本的
编辑(例如ed),sed通过
只输入一次通过
输入,因此更多的
效率。但它是sed的能力
过滤文本的流程,其中
特别区别于
其他类型的编辑器。

A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and is consequently more efficient. But it is sed's ability to filter text in a pipeline which particularly distinguishes it from other types of editors.

相关手册部分是此处。稍后解释如下

The relevant manual section is here. A small explanation follows


-i.bak支持就地编辑,并保留带有.bak扩展名的备份副本

-i.bak enables in place editing leaving a backup copy with .bak extension

s%foo%bar%使用替换命令s,其中
替换第一个字符串
在%sign,'foo'之间的匹配第二个
string,'bar'。它通常写为s //
,但因为你的字符串有大量的
的斜线,更方便的
改变它们为别的东西,所以你的
避免了逃避他们。

s%foo%bar% uses s, the substitution command, which substitutes matches of first string in between the % sign, 'foo', for the second string, 'bar'. It's usually written as s// but because your strings have plenty of slashes, it's more convenient to change them for something else so you avoid having to escape them.

示例


vinko@mithril:~$ sed -i.bak -e 's%C://temp%//home//some//blah%' a.txt
vinko@mithril:~$ more a.txt
//home//some//blah
D://temp
//home//some//blah
D://temp
vinko@mithril:~$ more a.txt.bak
C://temp
D://temp
C://temp
D://temp

这篇关于Linux命令将LARGE文件中的字符串替换为另一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆