Prepend first line of section to each line until the next section header

I have searched in a variety of ways in a variety of places but have come up empty.

I would like to prepend a portion of a section header to each following line until the next section header. I have been using sed for most things up until now but I'd go for a solution in just about anything-- awk, perl, python, tr, cat, you name it.

The files look like this, more or less:

file01.txt:

CUSTOMER: 233 IAMTHEBESTCUSTOMER
"ITEM32" "23" "UNITS" "20111003"
"ITEM106" "2" "CASES" "20111003"
CUSTOMER: 1024 CUSTOMERBESTISME
"ITEM32" "13" "CASES" "20111010"
"ITEM22" "12" "LB" "20111010"

And I'm trying to get to:

233 "ITEM32" "23" "UNITS" "20111003"
233 "ITEM106" "2" "CASES" "20111003"
1024 "ITEM32" "13" "CASES" "20111010"
1024 "ITEM22" "12" "LB" "20111010"

I read up on hold patterns in sed but I couldn't wrap my mind around them, was I barking up the wrong tree?

awk '/CUSTOMER:/{str=$2;next}{print str,$0}' file01.txt
1 Like

Does the $2 mean the second field delimited by spaces? That sure seems handy

---------- Post updated 2011-10-24 at 10:09 AM ---------- Previous update was 2011-10-23 at 07:08 PM ----------

That solution worked great, thank you! And such a quick reply too, I really appreciate it.

Yes...
$1 - first field
$2 - second field and so on

Default field separator is space. You can change it using -F option.

echo "1:2:3:4:5" | awk -F":" '{print $2}' 
2

--ahamed

Fields are normally separated by whitespace sequences (spaces, TABs, and newlines).

Thank you both. I have been banging my head against the wall on this one for literally weeks. I had a feeling it was terribly simple but I couldn't figure it out for the life of me.

Is there something I should have been searching for? I tried "prepend" "section" "header", etc etc but only came up with near misses.

your need provide the source data and the expect output again for your new problem.

I just meant that I probably could have found the solution you provided if I was searching for the correct terms, but I didn't know what I should be searching for. Kind of a chicken/egg thing I guess.

I was working on a different problem before and was having a heck of a time making any progress until I discovered that I wanted to do "tree traversal" at which point things got a lot easier. One of the perils of autodidactism I guess?