删除行范围\\ n个字符的文本文件 [英] Delete \n characters from line range in text file

查看:102
本文介绍了删除行范围\\ n个字符的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我们有1000行的文本文件。

Let's say we have a text file with 1000 lines.

我们如何从第20行中删除新行字符到500(例如与空格替换他们)?

How can we delete new line characters from line 20 to 500 (replace them with space for example)?

我尝试:

sed '20,500p; N; s/\n/ /;' #better not to say anything

所有其他线路(1-19&安培;&安培; 501-1000)。应该是preserved原样

All other lines (1-19 && 501-1000) should be preserved as-is.

由于我熟悉的sed,awk的或Perl的解决方案表示欢迎,但请为我是一个Perl和awk新手给他们一个交代。

As I'm familiar with sed, awk or perl solutions are welcomed, but please give an explanation with them as I'm a perl and awk newbie.

推荐答案

使用Perl的单行剥去新行:

Using a perl one-liner to strip the newline:

perl -i -pe 'chomp if 20..500' file

或与一个空格替换:

perl -i -pe 's/\R/ / if 20..500' file

说明:

开关


  • -i :编辑<> 到位文件(使得备份,如果提供的扩展名)

  • -p :创建一个而(LT;>){...;打印} 循环每个“行”的在你输入文件。

  • -e :告诉 perl的执行命令行上的code。

  • -i: Edit <> files in place (makes backup if extension supplied)
  • -p: Creates a while(<>){...; print} loop for each “line” in your input file.
  • -e: Tells perl to execute the code on command line.

code

  • chomp: Remove new line
  • 20 .. 500: if Range operator .. is between line numbers 20 to 500

这篇关于删除行范围\\ n个字符的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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