将文本转换成json格式

本文详细介绍了如何将结构化的文本数据转换为JSON格式,包括解析关键信息、组织数据结构和使用JSON库的方法,旨在帮助开发者高效地处理文本数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#!/bin/bash

format(){
        usedifs="$IFS"
        IFS=$'\n';[ $# -eq 0 ] && { echo "not files need format." ;exit 1; }
        for((i=1;i<=$#;i++));do files=(${files[@]} ${!i});done
        log1(){
                cat ${files[@]}|sed '/^[a-z]\+:/!d;s/[ \t]\+//g'|xargs -n3|\
                awk 'BEGIN{FS="[ :]"}
                {       x=$1;y=$3;z=$5
                        a[$2]+=$4
                        b[$2]=b[$2]?b[$2]","$NF:$NF
                }END{
                for(i in a){
                        split(b[i],t,",");
                        for(j in t)p["\""t[j]"\""]
                        for(k in p)v=v?v","k:k
                        print "{\""x"\":\""i"\",\""y"\":\""a[i]"\",\""z"\":["v"]}"
                        delete t
                        delete p
                        v=""}
                }'|paste -s -d ','
        }
        log2(){
                cat ${files[@]}|egrep '^([0-9]\.?){1,4}'|awk '{a[$1]+=$2}END{for(i in a){print "{\"ip\":\""i"\",\"times\":\""a[i]"\"}"}}'|paste -s -d ','
        }
        echo -e "{\n\t\"log1\":[$(log1)],\n\t\"log2\":[$(log2)]\n}"
        IFS="$usedifs"
}

format "*.log"


id:     766cbd
times:  6
type:   index, observe, forecast3d
id:     7c1429
times:  5459142
type:   index, forecast5d, observe
id:     334cfc
times:  144
type:   index, forecast3d, alarm
id:     549d81
times:  1775193
type:   forecast, index, observe, alarm, calendar
id:     71f520
times:  1638
type:   index, forecast3d, air

61.4.184.83     17480
61.4.184.81     1664154
61.4.184.93     5810487
61.4.184.91     5771866
61.4.184.82     17472
61.4.184.90     5757377
61.4.184.92     5798871


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值