在油灰码头的Grep和cut命令

时间:2014-11-04 15:47:47

标签: shell terminal grep

我正在尝试从多个文件中删除特定行。 我有大约300个json文件。

file structure:
{
"FieldName1": "XXXX",
"FieldName2": "1",
"FieldName3": "XXX",
"FieldName4": "XXX",
"FieldName5": "1",
.
.
.
}
,

 {
"FieldName1": "XXXX",
"FieldName2": "2",
"FieldName3": "XXX",
"FieldName4": "XXX",
"FieldName5": "2",
.
.
.
}

一些文件写在一行中:

{"FieldName1": "XXXX", "FieldName2": "3", "FieldName3": "XXX", "FieldName4": "XXX", "FieldName5": "3"}, {"FieldName1": "XXXX", "FieldName2": "4", "FieldName3": "XXX", "FieldName4": "XXX", "FieldName5": "4"}, ...


}

我需要在输出文件中获得的只是文件中2个字段的列表:

"FieldName1": "1",
"FieldName2": "1",
"FieldName1": "2",
"FieldName2": "2",
"FieldName1": "3",
"FieldName2": "3",
"FieldName1": "4",
"FieldName2": "4",
.
.
.

有一种简单的方法吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

对于单行,您可以使用此gnu awk(对RS中的多个字符执行)

awk -v RS='"FieldName' 'NR>1 {sub(/, "/,",");print RS$0}' file
"FieldName1": "XXXX",
"FieldName2": "3",
"FieldName3": "XXX",
"FieldName4": "XXX",

对于多行使用grepawk,请执行以下操作:

awk '/FieldName/' file
"FieldName1": "XXXX",
"FieldName2": "1",
"FieldName3": "XXX",
"FieldName4": "XXX",
"FieldName5": "1",