Chinaunix

标题: 帮忙处理一下数据,多谢了。 [打印本页]

作者: tstianzy    时间: 2013-12-04 21:25
标题: 帮忙处理一下数据,多谢了。
现在的格式:
report for localhost (192.168.11.16
details: Linux 2.6.32 - 3.9
report for localhost (192.168.11.15
report for localhost (192.168.11.160)
report for localhost (192.168.11.161)
details: Linux 2.6.32 - 3.9
report for localhost (192.168.11.163)
details: Windows XP

处理为:
192.168.11.168 Linux 2.6.32 - 3.9
192.168.11.158  
192.168.11.160
192.168.11.161 Linux 2.6.32 - 3.9
192.168.11.163 Windows XP

非常感谢了。
作者: yinyuemi    时间: 2013-12-04 21:38
本帖最后由 yinyuemi 于 2013-12-05 09:13 编辑

awk '/report/{printf (NR==1?"":RS) gensub("[()]","","g",$NF);next}{$1="";printf $0 }'  file
awk -vRS='report[^\n]+[(]' -F'[)\n]' 'NF{sub("details: ","");$1=$1;print}'
作者: reyleon    时间: 2013-12-04 21:50
本帖最后由 reyleon 于 2013-12-04 21:51 编辑
  1. grep -Po '(?<=\050).*(?=\051)|(?<=details: ).*' file | sed 'N;/\n[A-Za-z]\+/s/\n/ /;P;D'  
复制代码

作者: yestreenstars    时间: 2013-12-04 21:52
  1. [root@localhost ~]# awk '/^details:/{$1="";s=s""$0;next}{gsub(/[()]/,"");s=s?s"\n"$NF:$NF}END{print s}' i
  2. 192.168.11.168 Linux 2.6.32 - 3.9
  3. 192.168.11.158
  4. 192.168.11.160
  5. 192.168.11.161 Linux 2.6.32 - 3.9
  6. 192.168.11.163 Windows XP
  7. [root@localhost ~]#
复制代码

作者: yestreenstars    时间: 2013-12-04 22:03
本帖最后由 yestreenstars 于 2013-12-04 22:05 编辑
  1. [root@localhost ~]# sed 's/.*(\|)//g;N;s/\n.*://;P;D' i
  2. 192.168.11.168 Linux 2.6.32 - 3.9
  3. 192.168.11.158
  4. 192.168.11.160
  5. 192.168.11.161 Linux 2.6.32 - 3.9
  6. 192.168.11.163 Windows XP
  7. [root@localhost ~]#
复制代码

作者: uarenotalone    时间: 2013-12-05 00:23
回复 5# yestreenstars


   大神、敢不敢写的再简单点
作者: li0924    时间: 2013-12-05 09:36
  1. awk -F"[(:]" '/report/{s=gensub(/)/,"",1,$NF)}{if(/details/)a[s]=$NF;else a[s]=""}END{for(s in a)print s a[s]}'
复制代码

作者: jason680    时间: 2013-12-05 10:55
本帖最后由 jason680 于 2013-12-05 10:55 编辑

回复 1# tstianzy

Would you like this ...

$ $ awk -F'[:()]' 'NF>2{printf("%s",P$2);P="\n"}NF==2{printf $2}END{print""}' FILE
192.168.11.168 Linux 2.6.32 - 3.9
192.168.11.158
192.168.11.160
192.168.11.161 Linux 2.6.32 - 3.9
192.168.11.163 Windows XP

   
作者: 这个冬天不冷    时间: 2013-12-05 11:00
回复 8# jason680


   
作者: Herowinter    时间: 2013-12-05 14:41
回复 1# tstianzy
  1. awk '/localhost/{ip=gensub(/.*\((.*)\)/,"\\1",1);s=s?s"\n"ip:ip} /details/{sys=gensub("details: (.*)","\\1",1);s=s" "sys}END{print s}' i
复制代码

作者: lxzkenney    时间: 2013-12-05 17:42
提示: 作者被禁止或删除 内容自动屏蔽
作者: nibuduomaomao    时间: 2013-12-08 18:29
  1. sed -nr ':1;N;$s/report[^\(]*\(|\ndetails:|\)//gp;b1' help.txt
复制代码





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2