I’ve a file with header records followed by detail lines. I would like have the word from header tagged to the subsequent lines within the file using awk.. each Header record has a specific word ‘header’ in it.
My sample file is
h1_val header
this is line 1 under header set1
this is line 2 under header set1
this is line 3 under header set1
this is line 4 under header set1
h2_val header
this is line 1 under header set2
this is line 2 under header set2
this is line 3 under header set2
this is line 4 under header set2
My output should be like
h1_val this is line 1 under header set1
h1_val this is line 2 under header set1
h1_val this is line 3 under header set1
h1_val this is line 4 under header set1
h2_val this is line 1 under header set2
h2_val this is line 2 under header set2
h2_val this is line 3 under header set2
h2_val this is line 4 under header set2
Please help
Thanks!!!
Thanks ghoti it seems to be working fine if the lines are plain..
If my input looks like comma delimited and with double quotes… what variation should the awk be
"h1_val","header word"
"this is line 1","under header","set1"
"this is line 2","under header","set1"
"this is line 2","under header","set1"
"this is line 2","under header","set1"
"h2_val","header word"
"this is line 1","under header","set2"
"this is line 2","under header","set2"
"this is line 2","under header","set2"
"this is line 2","under header","set2"
Thanks!!
This seems to do it.
Or if you prefer, this is just about functionally equivalent:
Note that these imply that your header text has no spaces. If it does, then you may need to do something fancier than
$2=="header"to find your headers. If this is the case, then please update your question with more detail.