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

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

问题描述

我有被在服务器上执行一个巨大的SQL文件。转储是从我的机器,并在其中有关于我的机器一些设置。所以基本上,我想的C:// TEMP每次数是由//首页// //一些嗒嗒替换

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%富%的酒吧%用途秒,替换命令,该命令
  首先替换字符串匹配
  在%符号,'富',第二间
  字符串'吧。它通常写为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命令用另一个字符串替换字符串中的大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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