免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1467 | 回复: 3
打印 上一主题 下一主题

[文本处理] sed按关键字拆分一个文件 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2018-09-02 23:11 |只看该作者 |倒序浏览


我有一个文件想拆分为若干个xml文件

[R17:25:37.267][  DE01][0002882BA207D8F5E8E6C9F<?xml version="1.0" encoding="GBK" ?>
<ROOT>
  <HEAD>
    <VERSION></VERSION>
    <MSG_TYPE>1</MSG_TYPE>
    <MSG_REAL>0</MSG_REAL>
    <REQ_SAVE></REQ_SAVE>
    <GLOBAL_TASK_ID>2021071010010000010803</GLOBAL_TASK_ID>
    <SYS_ID></SYS_ID>
    <CHL_NO>1001</CHL_NO>

    .....................................

类似于这样,想按照<?xml version="1.0" encoding= >拆分为很多xml文件

使用sed 脚本如下

#!/bin/sh
File=./PktLog
count=0
line=(`sed -n '/?xml version="1.0" encoding/=' $File`)
num=${#line[@]}
for n in ${line[*]}
do
   [ $count -eq 0 ] && startLine=$n && continue
   let count+=1
   let endLine=n-1
   if [ $count -eq $num ]; then
      startLine=$n
      sed -n "${startLine},$ p" $File >result_${count}.txt
   else
      sed -n "${startLine},${endLine} p;q" $File >result_${count}.txt
      startLine=$n
   fi
done



但是脚本运行错误  请帮忙看看非常感谢。

spark@ubuntu:~$ sh split.sh
split.sh: 5: split.sh: Syntax error: "(" unexpected



论坛徽章:
0
2 [报告]
发表于 2018-09-03 09:23 |只看该作者
我调整bash后脚本可以正常运行。但是没有生成分割的文件。 调试脚本信息如下图

spark@ubuntu:~$ sh -x split.sh
+ File=/home/spark/PktLog
+ count=0
+ line=(`sed -n '/?xml version="1.0" encoding/=' $File`)
++ sed -n '/?xml version="1.0" encoding/=' /home/spark/PktLog
+ num=333
+ for n in '${line[*]}'
+ '[' 0 -eq 0 ']'
+ startLine=1
+ continue
+ for n in '${line[*]}'
+ '[' 0 -eq 0 ']'
+ startLine=137
+ continue
+ for n in '${line[*]}'
+ '[' 0 -eq 0 ']'
+ startLine=244
+ continue
+ for n in '${line[*]}'
+ '[' 0 -eq 0 ']'
+ startLine=415
+ continue
+ for n in '${line[*]}'
+ '[' 0 -eq 0 ']'
+ startLine=522
+ continue
+ for n in '${line[*]}'
+ '[' 0 -eq 0 ']'
+ startLine=674
+ continue
+ for n in '${line[*]}'
+ '[' 0 -eq 0 ']'
+ startLine=780
+ continue
+ for n in '${line[*]}'
+ '[' 0 -eq 0 ']'
+ startLine=932
.............................

论坛徽章:
0
3 [报告]
发表于 2018-09-03 15:04 |只看该作者
提示: 作者被禁止或删除 内容自动屏蔽

论坛徽章:
0
4 [报告]
发表于 2018-09-03 16:56 |只看该作者
  1. #!/bin/sh
  2. File=/home/spark/PktLog
  3. count=0
  4. startLine=(`sed -n -e '/?xml version="1.0" encoding/=' $File`)
  5. fileEnd=`sed -n '$=' $File`
  6. endLine=(`echo ${startLine[*]} | awk -v a=$fileEnd '{for(i=2;i<=NF;i++) printf("%d ",$i-1);print a}'`)

  7. let maxIndex=${#startLine[@]}-1

  8. for n in `seq 0 $maxIndex`

  9. do
  10.     sed -n "${startLine[$n]},${endLine[$n]}p" $File >result_${n}.xml
  11. done

  12. echo $startLine[@]

  13. 我用这个脚本也可以处理,生成大量的xml.

  14. 还有个问题请教:

  15. fileEnd=`sed -n '$=' $File`
  16. endLine=(`echo ${startLine[*]} | awk -v a=$fileEnd '{for(i=2;i<=NF;i++) printf("%d ",$i-1);print a}'`)
复制代码


能否给讲讲 endLine 和fileEnd 代码的意思,谢谢
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP