[英]Replace 2 lines in a file using sed command
I try to delete the comma on the next-to-last line: 我尝试删除倒数第二行上的逗号:
{ "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" } }, {
And I am using this command: 我正在使用以下命令:
<test2.file sed '/^\s*\},$/ { N; s/^\s*\},\n\{/^\s*\}\n\{/ }'
I expect the output should be like that: 我希望输出应该是这样的:
{ "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" } } {
But it returns an error. 但是它返回一个错误。
sed: -e expression #1, char 42: Unmatched {
sed:-e表达式#1,字符42:不匹配的{
Does anybody know what's wrong with it? 有人知道这是怎么回事吗? Thx.
谢谢。
PS, Here is the end of the file: 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" } }
don't escape { or }
in your pattern/address part. 不要在模式/地址部分中使用
{ or }
。 sed uses BRE by default. sed默认情况下使用BRE。 With BRE, chars like
{ ( + ..
don't have special meaning, you have to escape them to give them special meaning. 使用BRE,像
{ ( + ..
这样的字符没有特殊含义,您必须对其进行转义以赋予它们特殊的含义。
In your case, you want to match literal { or }
, then don't escape them. 对于您的情况,您想匹配文字
{ or }
,然后不要转义它们。
I guess you want to remove the last comma, so that change: 我猜您想删除最后一个逗号,以便进行更改:
...
},
{
into 成
...
}
{
then you can try: 那么您可以尝试:
awk -v RS='\0' '7+gsub(/,\s*{/,"\n{")' file
test with your 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.