简体   繁体   English

SED-组合匹配的正则表达式行以创建一个csv文件

[英]SED- combine matching regex lines to make a csv file

I was wondering if it is possible to use sed to create a csv file by combining multiple lines together onto a singe line separate by commas. 我想知道是否可以使用sed通过将多行组合到以逗号分隔的单行上来创建csv文件。

For example I have written a sed statement that retrieves the lines I want. 例如,我写了一个sed语句来检索我想要的行。

sed -n -e '/ENTITIES/,/ENDSEC/p' | sed -n -e '/  8/{n;p;}' -e '/ 10/{n;p;}' -e '/ 20/{n;p;}' -e '/ 11/{n;p;}' -e '/ 21/{n;p;}' < Test.txt > out.csv

Which produces the output; 产生输出;

 0
 4.93
 9.04
 27.9
 23.4
 0
 34.56
 0.77
 66.65
 19.50
 0
 55.26
 47.29
 53.42
 19.75
 0
 -18.22
 44.35
 19.74
 53.28

But I would Like it to output; 但是我想输出它;

 0,4.93,9.04,27.9,23.4
 0,34.56,0.77,66.65,19.50
 0,55.26,47.29,53.42,19.75
 0,-18.22,44.35,19.74,53.28

Is there anyway to do this without a pipe? 反正有没有管道吗? Id Rather not invoke another command as the files I process are upwards of 100 mil lines or so. 我宁愿不要调用其他命令,因为我处理的文件超过1亿行左右。

Thanks in advance for your help! 在此先感谢您的帮助!

To add, here is a portion of my input file; 要补充的是,这是我输入文件的一部分;

More Stuff Above 以上更多的东西

AcDbBlockEnd
  0
ENDSEC
  0
SECTION
  2
ENTITIES
  0
LINE
  5
1B1
330
1F
100
AcDbEntity
  8
0
100
AcDbLine
 10
4.933855223957067
 20
9.042372500389475
 30
0.0
 11
27.92566226775641
 21
23.49207557886149
 31
0.0
  0
LINE
  5
1B2
330
1F
100
AcDbEntity
  8
0
100
AcDbLine
 10
34.56437535704545
 20
0.778745874786317
 30
0.0
 11
66.65564369957746
 21
19.50612180407816
 31
0.0
  0
LINE
  5
1B3
330
1F
100
AcDbEntity
  8
0
100
AcDbLine
 10
55.26446832764479
 20
47.29118282642324
 30
0.0
 11
53.42718194719286
 21
19.75092411476788
 31
0.0
  0
LINE
  5
1B4
330
1F
100
AcDbEntity
ENDSEC
  0

More stuff below. 下面有更多内容。

Something like this might be what you're looking for, but as jaypal said, without seeing the input it's somewhat of a guess. 您可能正在寻找类似这样的东西,但是正如jaypal所说的,没有看到输入,这只是一个猜测。

sed -n '
  /ENTITIES/,/ENDSEC/p
  /  8/{n;h}
  / 10/{n;H}
  / 20/{n;H}
  / 11/{n;H}
  / 21/{n;H;g;s/\n/,/g;p}
' Test.txt > out.csv

With comments: 有评论:

sed -n '
  /ENTITIES/,/ENDSEC/p
  /  8/{n;h}       # store next line in hold space
  / 10/{n;H}       # append next line to hold space (after newline)
  / 20/{n;H}       # ditto
  / 11/{n;H}       # ditto
  / 21/{n;H;       # ditto
        g;         # put hold space into pattern space
        s/\n/,/g;  # substitute commas for newlines
        p          # print it
       }
' Test.txt > out.csv

Just pipe your sed to 只需将您的sed传送到

sed 'your long sed commnand' | paste -d, - - - - -

the result will be 结果将是

0,4.93,9.04,27.9,23.4
0,34.56,0.77,66.65,19.50
0,55.26,47.29,53.42,19.75
0,-18.22,44.35,19.74,53.28

Got it Thanks to ooga! 知道了,感谢ooga! Before I lacked the understanding of hold space vs. pattern space, now it has all become clear! 在我对保持空间与模式空间缺乏了解之前,现在一切都变得清楚了!

sed -n '
    /ENTITIES/,/ENDSEC/{
         /  8/{n;h;};
         / 10/{n;H;};
         / 20/{n;H;};
         / 11/{n;H;};
         / 21/{n;H;g;s/\n/,/g;p};
    }
' < Test.dxf > out.csv           

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM