如何在sed或awk中的两个模式之间连接线

我有一个json文件,我想在其中加入x括号之间的行数。

cat testfile.json
 {
  "CacheStatistics_ReadHits": 0,"CacheStatistics_ControllerPrefetchRemaining": [
   0,0
  ],"CacheStatistics_ControllerReadMisses": [
   0,0
  ]
 }

我尝试了以下操作,该操作应执行换行替换,首先从以'['结尾的行开始,一直持续到右括号']'。

sed -i '/\[$/,/\]/ s/\n//g' testfile.json

/\[$/搜索以空心括号结尾的行。
/\]/继续直到达到右括号。
s/\n//g用任何内容替换换行符

结果文件应为:

 {
  "CacheStatistics_ReadHits": 0,"CacheStatistics_ControllerPrefetchRemaining": [0,0],"CacheStatistics_ControllerReadMisses": [0,0]
 }
FLH123456789 回答:如何在sed或awk中的两个模式之间连接线

该程序将解决您的问题。您可以将其保存在名为0x58e54 = 0.0135 0x78707 = 0.0183

的文件中
myprogram.awk

您可以这样执行:

{
if($0 ~ /.*CacheStatistics_ControllerPrefetchRemaining/ ||  $0 ~ /.*CacheStatistics_ControllerReadMisses/){
        datos="";
        gsub("\\n","",$0); datos=datos$0;  
        getline; 
        gsub("\\n",$0); gsub(" ",$0); 
        datos=datos$0;
        getline; 
        gsub("\\n",$0); 
        datos=datos$0;
        getline;
        gsub(" ",$0);  
        datos=datos$0;
        print datos;
}
else
    print $0;

awk -f program.awk testfile.json 是您发布的数据。

我有以下输出:

testfile.json
本文链接:https://www.f2er.com/3157049.html

大家都在问