简体   繁体   English

python或bash - 在行尾添加“行和”

[英]python or bash - adding “ at beginning of line and ”, at end of line

I have text file with something like 我有类似的文本文件

first line
line nr 2
line three

etc 等等

And i want to generate 我想生成

"first line",
"line nr 2",
"line three",

I wonder how to do this in python or maybe in bash if it's easier/quicker. 我想知道如何在python中执行此操作,或者如果它更容易/更快,可能在bash中执行此操作。 I know there is different code for opening file and different for reading only one line in python(?) but i'm not sure which option to use in this case and, more importantly, how to add these characters. 我知道打开文件有不同的代码,在python(?)中只读取一行不同但我不确定在这种情况下使用哪个选项,更重要的是,如何添加这些字符。 Any advice would help. 任何建议都会有帮助。

sed 's/.*/"&",/'

For the reference, in case someone wants to do the same thing using python. 作为参考,万一有人想用python做同样的事情。 There is a handy module fileinput that could be used like this: 有一个方便的模块fileinput可以像这样使用:

import fileinput
import sys, os

for line in fileinput.input(inplace=True):
    sys.stdout.write('"%s",%s' % (line.rstrip(os.linesep), os.linesep))

Then run this as a script: 然后将其作为脚本运行:

python myscript.py file1 file2 file3

That will change the files inplace for you. 这将为您改变文件。

Be a true unix geek: use sed! 成为一名真正的unix极客:使用sed!

sed 's/^/"/; s/$/",/;' < your_text_file

If you want to escape existing double quotes with backslashes, use 's/"/\\\\"/g; s/^/"/; s/$/",/;' 如果要使用反斜杠转义现有的双引号,请使用's/"/\\\\"/g; s/^/"/; s/$/",/;' 's/"/\\\\"/g; s/^/"/; s/$/",/;' as the pattern. 作为模式。

sed is ideally suited for this type of task. sed非常适合这类任务。 Check out a ludicrously long list of examples . 查看一个非常长的一系列示例

there is no need to construct regular expression(with backreferencing) for this task. 没有必要为此任务构造正则表达式(使用反向引用)。 Its an expensive operation since you are not going to change something in the line. 它是一个昂贵的操作,因为你不会改变行中的东西。 Easiest way is just to print them out. 最简单的方法就是将它们打印出来。

    awk '{print "\042"$0"\042,"}' file 

Results on operation on a big file: 在大文件上操作的结果:

$ head -5 file
this is line
this is line
this is line
this is line
this is line
$ wc -l < file
9545088

$ time  awk '{print "\042"$0"\042,"}' file  >/dev/null

real    0m15.574s
user    0m15.327s
sys     0m0.172s

$ time sed 's/.*/"&",/' file > /dev/null

real    0m31.717s
user    0m31.465s
sys     0m0.157s

$ time perl -p -e 's/^(.*)$/\"$1\",/g'  file >/dev/null

real    0m36.576s
user    0m36.006s
sys     0m0.360s

A number of easy ways to do it... 一些简单的方法来做到这一点......

A simple perl oneliner: 一个简单的perl oneliner:

perl -pi -e 's/^(.*)$/\"$1\",/g' /path/to/your/file

To explain a bit, the regex ^(.*)$ grabs everything (the (.*) ) between the start of the line ( ^ ) and the end of the line ( $ ), then uses the $1 match group variable to reconstruct it with the quotes and comma. 为了解释一下,正则表达式^(.*)$抓取行开头( ^ )和行结束( $ )之间的所有内容( (.*) ),然后使用$1匹配组变量进行重构它带引号和逗号。

In Bash: 在Bash:

while read line
    do
    echo "\"${line}\","
done < inputfile

Python 蟒蛇

for line in open("file"):
  line=line.strip()
  print '"%s",'  % line

sh + awk are nice here too... sh + awk在这里也很不错......

!/bin/sh
for FILE in "$@"
do
   awk '{print "\" $0 "\","}' < $FILE > $FILE.tmp
   mv $FILE.tmp $FILE
done

在vi中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM