替换文本文件中的第n行 [英] Replace nth line in a text file

查看:124
本文介绍了替换文本文件中的第n行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何替换R中文本文件的第n行?

How do I go about in replacing the nth line of a text file in R?

推荐答案

要替换第三行:

$ cat junk.txt
sic transit
gloria mundi
temeo danoas
et dona ferentes

执行此操作:

> latin = readLines("junk.txt",-1)
> latin[3]="per ardua ad astra"
> writeLines(latin,"junkout.txt")

并获得:

$ cat junkout.txt 
sic transit
gloria mundi
per ardua ad astra
et dona ferentes

如果需要,可以writeLines(latin,"junk.txt")并覆盖输入文件.

You can writeLines(latin,"junk.txt") and overwrite the input file if you want.

这篇关于替换文本文件中的第n行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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