Monday 15 July 2013

regex - sed: Dynamically remove all text columns except positions defined by pattern -



regex - sed: Dynamically remove all text columns except positions defined by pattern -

by searching , trying (no regex expert), have managed process text output using sed or grep, , extract lines, formatted way:

tree number 280: 1 0.500 1 node_15 6 --> h 1551.code 1 node_21 s ==> h node_20 tree number 281: 1 0.500 1 node_16 s ==> m 1551.code 1 node_20 s --> h node_19

then, using

sed 's/^.\{35\}\(.\{9\}\).*/\1/' infile , desired part, plus output rid of later (not problem).

tree number 280: 6 --> h s ==> h tree number 281: s ==> m s --> h

however, horizontal position of c --> c pattern may vary file file, although aligned. there way extract --> or ==> including single preceeding , next characters, no matter columns found in?

the tree number # part not necessary , left blank well, there has separator of kind.

update (alternative approach)

trying utilize grep, issued

grep -eo '(([a-za-z0-9] -- |[a-za-z0-9] ==)> [a-za-z0-9]|changes)' infile.

a sample of initial file follows, if thinks of better, more efficient approach, or utilize of regex insane, please comment!

..misc text... character alter lists: character ci steps changes ---------------------------------------------------------------- 1 0.000 1 node_235 h --> s node 1 node_123 s ==> 6 1843 1 node_126 s ==> h 2461 1 node_132 s ==> 6 1863 1 node_213 h --> 1816 1 node_213 h --> 8 1820 ..ct... character alter lists: character ci steps changes ---------------------------------------------------------------- 1 0.000 1 node_165 h --> s node 1 node_123 s ==> 6 1843 1 node_231 h ==> s 1823 ..misc text...

grep bit easier extracting matching regex (if need different separators can add together them list separated pipes [-|=]

grep -o '. [-|=][-|=]> .' infile

of if want sed this, should first part matches lines have pattern , sec part extracts matching regex

sed -n '/[--|==]>/{s/.*\(. [=|-][-|=]> .\).*/\1/p}' infile

regex bash awk sed

No comments:

Post a Comment