如何创建正则表达式以获取字符串列表?

时间:2016-06-22 15:36:23

标签: regex bash

你好我正在处理一个如下文件:

AliceBlue
AntiqueWhite
Aqua
Aquamarine
Azure
Beige
Bisque
Black
BlanchedAlmond
Blue
BlueViolet
Brown

我想从另一个列表中获得具有以下结构的列表:

"AliceBlue","AntiqueWhite","Aqua",...,

为了达到这个目的,我试过了:

awk -vORS=, '{ print $1 }' listColors.txt | sed 's/,$/\n/'

我得到了:

AliceBlue,AntiqueWhite,..., 

我几乎实现了我想要的但我认为我需要构建一个正则表达式来为所有单词添加双引号,我不是正则表达式的专家,我想欣赏任何完成任务的建议。

1 个答案:

答案 0 :(得分:3)

使用awk即可:

awk -v ORS=, '{$1= "\"" $1 "\""} 1' file; echo

"AliceBlue","AntiqueWhite","Aqua","Aquamarine","Azure","Beige","Bisque","Black","BlanchedAlmond","Blue","BlueViolet","Brown",