在文件中每行的开头和结尾添加字符 [英] Adding characters at the start and end of each line in a file

查看:50
本文介绍了在文件中每行的开头和结尾添加字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在每行的开头和结尾添加一些字符的最佳方法是什么?可以使用 Vim 或其他方式完成吗?

What is the best way to add some characters at the start and end of each line? Can it be done using Vim, or some other way?

推荐答案

在 vim 中,你可以这样做

In vim, you can do

:%s/^\(.*\)$/"\1"/

  • s/regex/replace/ 是用于搜索 n 替换的 vim 命令.
  • % 使其适用于整个文件
  • ^$ 分别表示行的开始和结束.
  • (.*) 捕获两者之间的所有内容.() 需要在 vim 正则表达式中转义.
  • \1 将捕获的内容放在两个引号之间.
    • s/regex/replace/ is vim command for search n replace.
    • % makes it apply throughout the file
    • ^ and $ denotes start and end of the line respectively.
    • (.*) captures everything in between. ( and ) needs to be escaped in vim regexes.
    • \1 puts the captured content between two quotes.
    • 这篇关于在文件中每行的开头和结尾添加字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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