Awk sed命令删除与模式(sed)匹配的行块(上下)-使用Python重新求解
请参阅下面的链接 因为我是“sed”的新手, 我有一个文件(my_file.json)的内容如下;我需要删除从“{”到“}”的所有行 我使用以下命令;它帮助我删除块的下半部分,即直到“}”的图案之后,以及具有图案的线及其上方的线Awk sed命令删除与模式(sed)匹配的行块(上下)-使用Python重新求解,awk,sed,Awk,Sed,请参阅下面的链接 因为我是“sed”的新手, 我有一个文件(my_file.json)的内容如下;我需要删除从“{”到“}”的所有行 我使用以下命令;它帮助我删除块的下半部分,即直到“}”的图案之后,以及具有图案的线及其上方的线 sed -i '/my_script.py"/I,+2 d;$!N;/my_script.py"/!P;D' my_file.json 输出结果如下所示 [ { "use"
sed -i '/my_script.py"/I,+2 d;$!N;/my_script.py"/!P;D' my_file.json
输出结果如下所示
[
{
"use":"abcd",
{
"use":"abcd"
"contact":"xyz",
"name":"some_other_script.py",
"time":"11:22:33"
},
{
"use":"apqwkndf",
{
"use":"kjdshfjkasd",
]
预期产出为:;请注意,因为它只有一个街区剩余,所以我需要删除“,”
我如何解决这个问题?这可能对您有用(GNU-sed):
这将删除不需要的列表元素,然后修复最后一个列表分隔符
另一种方法是将编辑的文件存储在内存中,然后修复最后一个分隔符:
sed '/{/{:a;N;/}/!ba;/my_script\.py/d};H;$!d;x;s/.//;s/\(.*}\),\(\s*]\)/\1\2/' file
请您在一个单独的
awk
中尝试以下内容,像json这样的公平警告输入文件应该由jq
之类的工具编辑或读取,因为OP说他不允许使用,所以添加了这个。它完全由显示的样本只写
awk '
/{/{
found=1
if(noPrint==""){
actualVal=(actualVal?actualVal ORS:"")val
}
val=noPrint=""
}
found && /"name":"my_script.py"/{
noPrint=1
}
{
val=(val?val ORS:"")$0
}
END{
if(noPrint==""){
actualVal=(actualVal?actualVal ORS:"")val
}
sub(/},$/,"}\n]",actualVal)
print actualVal
}
' Input_file
处理此类问题的通常方法是:
cat <<EOF |
[
{
"use":"abcd",
"contact":"xyz",
"name":"my_script.py",
"time":"11:22:33"
},
{
"use":"abcd",
"contact":"xyz",
"name":"some_other_script.py",
"time":"11:22:33"
},
{
"use":"apqwkndf",
"contact":"xyz",
"name":"my_script.py",
"time":"11:22:33"
},
{
"use":"kjdshfjkasd",
"contact":"xyz",
"name":"my_script.py",
"time":"11:22:33"
}
]
EOF
sed -n '
b noterror ; : error {
s/.*/ERROR: &/
q1
} ; : noterror
# remove [ ]
1d;$d;
# first line should be open braces
/{/!{b error}
# read up until closing brackets
# Note escaping is not handled
: again {
N;
$b error
/}/!b again
}
s/}.*/}/;
s/\n/ /g;
# -- one information per line --
p
' | awk '
# filter that myscript.py with a regex
!/"name" *: *"my_script.py"/{
# output with those [ ]
printf "[\n"
print # print the line
printf "]\n"
}'
您可能希望恢复换行符,方法是将一个特殊字符替换为换行符,然后将该字符替换回换行符,或者对awk
到。请添加您的预期输出。添加了预期输出output@Abhishek,如果这是一个json文件,那么专家建议SO使用熟悉如何解析的工具解析json。您是否可以使用像
jq
这样的工具?或者您的系统中有它?不,我不能使用jq,因为我是“sed”的新手,所以不要使用它。还有其他更好的工具。对于json,请使用支持json的工具。您不能使用jq
?使用python
或perl
。
sed '/{/{:a;N;/}/!ba;/my_script\.py/d};H;$!d;x;s/.//;s/\(.*}\),\(\s*]\)/\1\2/' file
awk '
/{/{
found=1
if(noPrint==""){
actualVal=(actualVal?actualVal ORS:"")val
}
val=noPrint=""
}
found && /"name":"my_script.py"/{
noPrint=1
}
{
val=(val?val ORS:"")$0
}
END{
if(noPrint==""){
actualVal=(actualVal?actualVal ORS:"")val
}
sub(/},$/,"}\n]",actualVal)
print actualVal
}
' Input_file
cat <<EOF |
[
{
"use":"abcd",
"contact":"xyz",
"name":"my_script.py",
"time":"11:22:33"
},
{
"use":"abcd",
"contact":"xyz",
"name":"some_other_script.py",
"time":"11:22:33"
},
{
"use":"apqwkndf",
"contact":"xyz",
"name":"my_script.py",
"time":"11:22:33"
},
{
"use":"kjdshfjkasd",
"contact":"xyz",
"name":"my_script.py",
"time":"11:22:33"
}
]
EOF
sed -n '
b noterror ; : error {
s/.*/ERROR: &/
q1
} ; : noterror
# remove [ ]
1d;$d;
# first line should be open braces
/{/!{b error}
# read up until closing brackets
# Note escaping is not handled
: again {
N;
$b error
/}/!b again
}
s/}.*/}/;
s/\n/ /g;
# -- one information per line --
p
' | awk '
# filter that myscript.py with a regex
!/"name" *: *"my_script.py"/{
# output with those [ ]
printf "[\n"
print # print the line
printf "]\n"
}'
[
{ "use":"abcd", "contact":"xyz", "name":"some_other_script.py", "time":"11:22:33" }
]