如何根据某些条件拆分csv文件并写入新文件?

时间:2014-07-03 07:04:59

标签: perl awk

我有一个像下面这样的文件,我需要获取标题的值(在文件中多次出现)并将其存储在单独的文件中

{"card":{"cardName":"43SCX4","portSignalRates":["43SCX4-L-OTU3","43SCX4-C-TENGIGE","43SCX4-C-OTU2","43SCX4-C-FC8G","43SCX4-C-STM64","43SCX4-C-OC192"],"listOfPort":{"43SCX4-L-OTU3":{"portAid":"43SCX4-L-OTU3","signalType":"OTU3","tabNames":["PortDetails","OTU3e2Details"],"title":"OperationalMode",{"label":"Regen","value":"regen"}],"label":"Regen","value":"regen","checked":"","enabled":"true","selected":""},{"type":"dijit.form.Select","name":"Frequency","title":"Transmit Frequency "}}}}

我尝试使用“awk -F,'{''示例”,我可以拆分,但不能迭代并将其放到另一个文件中“title”:“ **** < / strong>“

1 个答案:

答案 0 :(得分:0)

通过grep with -oE选项。

$ grep -oE '\"title\":\"[^"]*"' file
"title":"OperationalMode"
"title":"Transmit Frequency "

如果你想获得Title的值,那么使用lookbehind,

$ grep -oP '(?<=\"title\":\")[^"]*' file
OperationalMode
Transmit Frequency 

如果要将其另存为另一个文件,请使用输出重定向>运算符将输出重定向到另一个文件。示例grep -oP '(?<=\"title\":\")[^"]*' file > outfile