2013-09-20

Concatenating two specific lines in sed

I wanted to concatenate specific rows of data so that I could analyse and chart them in Excel. My data had rows starting with a date and a keyword:

01/01/2013
12
46
79
xyz 0.1
02/01/2013
56
23
xyz 0.5

And the output I wanted is:

01/01/2013 xyz 0.1
02/01/2013 xyz 0.5

Below is a sed script to concatenate those two lines and ignore the others:

# Use: sed -n -f Concat.sed test.log
# H Hold <- Hold + \n + Pattern
# h Hold <- Pattern
# g Pattern = Hold
# First pattern
/..\/..\/..../ {
 h
}
# Second pattern
/xyz/ {
 H
 g
 s/\n/ /
 p
}

The "trick" is to collect the rows that I need into the hold space first then transfer it to the pattern space where the line break can be replaced by a space.

No comments:

Post a Comment