Chinaunix

标题: 求助,急 [打印本页]

作者: qzhu    时间: 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
作者: dbcat    时间: 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
复制代码

作者: dbcat    时间: 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
复制代码

作者: qzhu    时间: 2006-03-22 02:15
能不能简单解释一下h,D, G 以及最后一行的含义,谢谢
作者: 寂寞烈火    时间: 2006-03-22 02:18
http://bbs.chinaunix.net/viewthread.php?tid=336126
BTW:注意论坛发贴要求
作者: qzhu    时间: 2006-03-22 08:46
还是awk简单一点
NF == 1 { name=$1}
NF > 1 {print name, $1, $NF}

然后再把所有的"/"去掉就行了




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