Unix命令行:如何为单词添加双引号?

时间:2014-08-05 13:49:05

标签: linux unix sed command

我有一个文本文件" names.txt"它包含一个像这样的单词列表:

Adidas
Android
Bluetooth
Minaret
Mushroom
acorn
airplane
amazon

我希望这个文件完全像:

"Adidas" = "";
"Android" = "";
"Bluetooth" = "";
"Minaret" = "";
"Mushroom" = "";
"acorn" = "";
"airplane" = "";
"amazon" = "";

知道怎么做到这一点?谢谢

4 个答案:

答案 0 :(得分:2)

这个sed怎么样?

$ sed -e 's/^/"/' -e 's/$/" = "";/' file
"Adidas" = "";
"Android" = "";
"Bluetooth" = "";
"Minaret" = "";
"Mushroom" = "";
"acorn" = "";
"airplane" = "";
"amazon" = "";

它将行{(1}})的开头替换为^,行尾(")替换为$。因此,它最终会在开头添加" = "";,在结尾添加"

使用" = "";进行编辑。


你也可以使用bash:

sed -i.bak '...'

或者awk:

while read line
do
    printf "%s%s%s\n" '"' $line '" = "";'
done < file

答案 1 :(得分:2)

使用此sed命令:

sed 's/.*/"&" = "";/'

要修改文件,您可以这样做:

sed 's/.*/"&" = "";/' filename > tmp && mv tmp filename

(选择一个不存在的临时文件名;我经常使用$$,当前shell的进程ID)或:

sed -i.bak 's/.*/"&" = "";/' filename

后者将原始文件复制到filename.bak

答案 2 :(得分:0)

awk方式

awk '$0="\""$0"\" = \"\";"' file

Sed方式

sed 's/.*/"&" = "";/' file

答案 3 :(得分:0)

awk -v ORS='" = "";\n' '$1="\""$1'