linux终端删除文件命令_如何使用Linux终端从文件中间删除行

linux终端删除文件命令

linux终端删除文件命令

When you manage your own servers, one of the things you end up needing to do on a semi-regular basis is extract stuff from the middle of a file. Maybe it’s a log file, or you need to extra a single table from the middle of your MySQL backup file, like I did.

当您管理自己的服务器时,最终需要半定期执行的一件事是从文件中间提取内容。 也许这是一个日志文件,或者像我一样,您需要在MySQL备份文件的中间添加一个表。

To figure out the line numbers, a simple grep -n command did the job (the -n argument outputs the line numbers). This made it easy to figure out what I needed to extract.

为了弄清行号,一个简单的grep -n命令完成了该工作(-n参数输出行号)。 这样就很容易弄清楚我需要提取的内容。

grep -n wp_posts howtogeekdb010114.bak | more
grep -n wp_posts howtogeekdb010114.bak | more

Results in something like this, which shows the line numbers over on the left side of the output. Piping everything into “more” makes sure that you can see the first line without it scrolling by. Now you’ve got the line number to start with, and probably the one to end with.

结果是这样,它在输出的左侧显示了行号。 将所有内容添加到“更多”中可确保您无需滚动就可以看到第一行。 现在,您已经有了行号,也可能以行号结尾。

4160:-- Table structure for table `wp_posts`
4163:DROP TABLE IF EXISTS `wp_posts`;
4166:CREATE TABLE `wp_posts` (
4203:-- Dumping data for table `wp_posts`
4206:LOCK TABLES `wp_posts` WRITE;
4207:/*!40000 ALTER TABLE `wp_posts` DISABLE KEYS */;
4208:INSERT INTO `wp_posts` VALUES (1,2,'2006-09-11 05:07:23','2006-09-11

You could, of course, just pipe the output from grep into another file, like this:

当然,您可以将grep的输出传递到另一个文件中,如下所示:

grep keyword filename.txt > outputfile
grep keyword filename.txt > outputfile

In my case, that didn’t want to work, because I couldn’t import the resulting backup for some reason. So, I found a different way to extract the lines using sed, and this method did work.

就我而言,那是不希望的,因为由于某种原因我无法导入结果备份。 因此,我找到了另一种使用sed提取行的方法,该方法确实有效。

sed -n '4160,4209p' howtogeekdb0101140201.bak > outputfile

Basically the syntax is like this, making sure to use the -n argument, and include the “p” after the second line number.

基本上,语法是这样的,请确保使用-n参数,并在第二行号之后包含“ p”。

sed -n 'FIRSTLINENUMBER, LASTLINENUMBERp' filename > outputfilename
sed -n 'FIRSTLINENUMBER, LASTLINENUMBERp' filename > outputfilename

Some other ways you can pull out specific lines in the middle of a file? You could use the “head” command with the +number argument to just read through the first x lines of a file, and then use tail to extract those lines. Not the best option, lots of overhead. Simpler option? You can use the split command to turn the file into multiple files right at the line number you want, and then extract the lines using head or tail.

您可以通过其他方法在文件中间提取特定行吗? 您可以将“ head”命令与+ number参数一起使用,以仅读取文件的前x行,然后使用tail提取这些行。 不是最好的选择,很多开销。 更简单的选择? 您可以使用split命令将文件正确地转换为所需行号的多个文件,然后使用头或尾提取行。

Or you can just use sed.

或者您可以只使用sed。

翻译自: https://www.howtogeek.com/178838/how-to-remove-lines-from-the-middle-of-a-file-using-the-linux-terminal/

linux终端删除文件命令

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值