[英]Replace 2 lines in a file using sed command
我嘗試刪除倒數第二行上的逗號:
{ "subject" : { "value" : "http://d-nb.info/gnd/1-2", "type" : "uri" }, "predicate" : { "value" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", "type" : "uri" }, "object" : { "value" : "http://d-nb.info/standards/elementset/gnd#SeriesOfConferenceOrEvent", "type" : "uri" } }, {
我正在使用以下命令:
<test2.file sed '/^\s*\},$/ { N; s/^\s*\},\n\{/^\s*\}\n\{/ }'
我希望輸出應該是這樣的:
{ "subject" : { "value" : "http://d-nb.info/gnd/1-2", "type" : "uri" }, "predicate" : { "value" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#type", "type" : "uri" }, "object" : { "value" : "http://d-nb.info/standards/elementset/gnd#SeriesOfConferenceOrEvent", "type" : "uri" } } {
但是它返回一個錯誤。
sed:-e表達式#1,字符42:不匹配的{
有人知道這是怎么回事嗎? 謝謝。
PS,這是文件的結尾:
{ "subject" : { "value" : "http://d-nb.info/gnd/1060867974", "type" : "uri" }, "predicate" : { "value" : "http://d-nb.info/standards/elementset/gnd#preferredNameEntityForThePerson", "type" : "uri" }, "object" : { "value" : "_:genid12768016", "type" : "bnode" } }
不要在模式/地址部分中使用{ or }
。 sed默認情況下使用BRE。 使用BRE,像{ ( + ..
這樣的字符沒有特殊含義,您必須對其進行轉義以賦予它們特殊的含義。
對於您的情況,您想匹配文字{ or }
,然后不要轉義它們。
我猜您想刪除最后一個逗號,以便進行更改:
...
},
{
成
...
}
{
那么您可以嘗試:
awk -v RS='\0' '7+gsub(/,\s*{/,"\n{")' file
測試您的文件:
kent$ awk -v RS='\0' '7+gsub(/,\s*{/,"\n{")' f
{
"subject" : {
"value" : "http://d-nb.info/gnd/1-2",
"type" : "uri"
},
"predicate" : {
"value" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
"type" : "uri"
},
"object" : {
"value" : "http://d-nb.info/standards/elementset/gnd#SeriesOfConferenceOrEvent",
"type" : "uri"
}
}
{
您可以使用縮進識別正確的行嗎?
sed 's/^ },/ }/'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.