andy_egor_johnson
andy_egor_johnson

Reputation: 7

SED / RegEx Puzzle

I have a file, with many of the log lines in like below example, what i'd like to do is basically add a CR after each piece of process information. I figured i'd do this with SED using the below

sed -rn 's/([0-9]+) \(([a-z._0-9]+)\) ([0-9]+) ([0-9]+)/ \2,\1,\3,\4 \n/gp' < file

This partially works, but I still get the Total: 3266 #015 from the log, which appears at the end of each line. I didn't expect this as it doesn't get matched in the regular expression.

I've tested the regular expression on the available websites, and they always look good, and find what i'd expect, its just when i combine with SED i don't quite get the result i was expecting.

Any help or suggestions would be most appreciated,

Thanks

Andy

This is a single line of the stats

1 (init) 3686400 123   148 (klogd) 3690496 116  16364 (memlogger.sh) 3686400 144  17 0  225 (dropbear) 1847296 113  242 (mini_httpd) 2686976 167  281 (snmpd) 4812800 231  283 (logmuxd) 2514944 262  284 (watchdog) 3551232 82  285  (controld) 5259264 610  287 (setupd) 5120000 436  289 (checkpoold) 3424256 129  296 (trap_sender_d) 3457024 165  298 (watch) 3686400 114   299 (processwatchdog) 3420160 119  314 (timerd) 3637248 219  315 (init) 3686400 116  16365 (cat) 3694592 120  Total:  3266 #015

Upvotes: 0

Views: 121

Answers (1)

choroba
choroba

Reputation: 241898

Just remove the "Total:"

sed  -rn 's/ +Total:.*//;
          s/([0-9]+) +\(([a-z._0-9]+)\) +([0-9]+) +([0-9]+)/ \2,\1,\3,\4\n/gp'

You can also match the "Total:" optionally:

sed  -rn 's/([0-9]+) +\(([a-z._0-9]+)\) +([0-9]+) +([0-9]+)( *Total:.*)?/ \2,\1,\3,\4\n/gp'
#                                                          ------------^

Upvotes: 1

Related Questions