我有一个文本文件的输出,如下所示。我想将someItems数组的所有内容放在一行之下。因此,每一行都会有一个新的someItems数组的内容。例如:
"someItems": [
{
"someId": "MountSomers-showtime.com-ETTI0000000000000003-1452005472058",
"source": "MountSomers",
"sourceAssetId": "9",
"title": "Pk_3",
"ppp": "12",
"expirationDate": "2016-01-06T14:51:12Z"
}, {
"someId": "MountSomers-ericsson.com- ETTI0000000000000005-1452005472058",
"source": "MountSomers",
"sourceAssetId": "12",
"title": "Pk_5",
"ppp": "12",
"expirationDate": "2016-01-06T14:51:12Z"
} ]
"someItems": [
{
"someId": "MountSomers-hbo.com-ETTI0000000000000002-1452005472058",
"source": "MountSomers",
"sourceAssetId": "7",
"title": "Pk_2",
"ppp": "12",
"expirationDate": "2016-01-06T14:51:12Z"
}, {
"someId": "MountSomers-showtime.com-ETTI0000000000000003-1452005472058",
"source": "MountSomers",
"sourceAssetId": "9",
"title": "Pk_3",
"ppp": "12",
"expirationDate": "2016-01-06T14:51:12Z"
}, {
"someId": "MountSomers-ericsson.com-ETTI0000000000000005-1452005472058",
"source": "MountSomers",
"sourceAssetId": "12",
"title": "Pk_5",
"ppp": "12",
"expirationDate": "2016-01-06T14:51:12Z"
} ]
会变成
"someItems": [ ..... ]
"someItems": [ ..... ]
我有以下
cat file | | awk '/^"someItems": [/{p=1}/^]/{p=0} {if(p)printf "%s",$0;else printf "%s%s\n",(NR==1?"":RS),$0}'
但它没有做我想要的......
答案 0 :(得分:1)
由于输入仅在外层包含括号[]
,因此解决方案非常简单:
awk '{gsub("\n","", $0)}1' RS=']\n' file
我使用]\n
作为输入记录分隔符。这为您提供了"someItems: ..."
到结束]
之间$0
的整个部分。 gsub()
只是替换换行符。 1
打印(修改过的)记录。
您还可以使用sed
:
sed '/\[/{:a;N;/]/!ba;s/\n//g}' file
我将在多行版本中解释它:
script.sed :
# Address. Matches a line containing the opening [
/\[/ { # Start of block
# Define a label 'a'
:a
# Read a new line and append it to the pattern buffer
N
# If the pattern buffer doens't contain the closing ]
# jump back to label 'a'
/]/!ba
# Replace all newlines once the closing bracket appeared
# Since we don't jump back to 'a' in this case, this means we'll
# leave the block and start a new cycle.
s/\n//g
} # End of block
答案 1 :(得分:0)
$ awk '/^"someItems":/ && f { printf "\n" } { printf $0; f=1 } END { printf "\n" }' file.txt
"someItems": [{ "someId": "MountSomers-showtime.com-ETTI0000000000000003-1452005472058", "source": "MountSomers", "sourceAssetId": "9", "title": "Pk_3", "ppp": "12", "expirationDate": "2016-01-06T14:51:12Z"}, { "someId": "MountSomers-ericsson.com- ETTI0000000000000005-1452005472058", "source": "MountSomers", "sourceAssetId": "12", "title": "Pk_5", "ppp": "12", "expirationDate": "2016-01-06T14:51:12Z"} ]
"someItems": [{ "someId": "MountSomers-hbo.com-ETTI0000000000000002-1452005472058", "source": "MountSomers", "sourceAssetId": "7", "title": "Pk_2", "ppp": "12", "expirationDate": "2016-01-06T14:51:12Z"}, { "someId": "MountSomers-showtime.com-ETTI0000000000000003-1452005472058", "source": "MountSomers", "sourceAssetId": "9", "title": "Pk_3", "ppp": "12", "expirationDate": "2016-01-06T14:51:12Z"}, { "someId": "MountSomers-ericsson.com-ETTI0000000000000005-1452005472058", "source": "MountSomers", "sourceAssetId": "12", "title": "Pk_5", "ppp": "12", "expirationDate": "2016-01-06T14:51:12Z"} ]
$
打印每一行没有尾随换行符。从第二次出现开始,在每个"someItems"
之前放置一个前导换行符。在最后打印换行符以保持优雅。