免费注册 查看新帖 |

Chinaunix

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

求助,急 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-03-21 09:27 |只看该作者 |倒序浏览
Hi,

I need to parse a file to a new format:

source file format is like this:
/ABC
abc 0 0 0 0 0 1
efg 0 0 0 0 0  3

/XYZ
cde 0 0 0 0 0 1
fgh 0 0 0 0 0  1
uvw 0 0 0 0 0 2

......

need to parse it to a new file like this:
ABC abc  1
ABC efg  3
XYZ cde  1
XYZ fgh   1
XYZ uvw  2
...

basically what need to be done is to get the name after the slash "/" and  combine it with the first and last column of lines immediately after it.
I know a little bit of awk and sed but not enough to solve this problem, any help or hint with a piece of code will be appreciated.

Thanks,
Qing

论坛徽章:
0
2 [报告]
发表于 2006-03-21 09:58 |只看该作者

  1. sed '
  2. s/ 0//g
  3. /\//h
  4. s/^\/.*//
  5. /\//!{
  6. G
  7. s/\n//
  8. s/^\/.*//
  9. s/\(.*\)\/\(.*\)/\2 \1/
  10. }
  11. ' file
复制代码

论坛徽章:
0
3 [报告]
发表于 2006-03-21 12:10 |只看该作者

修改一下,有点错误:

  1. sed '
  2. /^$/d
  3. /^\//{
  4. h
  5. D
  6. }
  7. {
  8. G
  9. s/\n//
  10. s/\([^ ]*\)\(.*\)\(.\)\/\(.*\)/\4 \1 \3/
  11. }
  12. ' file
复制代码

论坛徽章:
0
4 [报告]
发表于 2006-03-22 02:15 |只看该作者
能不能简单解释一下h,D, G 以及最后一行的含义,谢谢

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
5 [报告]
发表于 2006-03-22 02:18 |只看该作者

论坛徽章:
0
6 [报告]
发表于 2006-03-22 08:46 |只看该作者
还是awk简单一点
NF == 1 { name=$1}
NF > 1 {print name, $1, $NF}

然后再把所有的"/"去掉就行了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP