Replacing lines containing a pattern with sedThanks to @don_crissti and @Archemar. I reckon my question is not a duplication of the above question.So here is an example of input. Edit: All sections have values. I've added another complexity to the subheading (I should have done that). DATA1NAME: A (X)12,113,214,4NAME: B (X)21,222,3NAME: C (X)24,4The desired output should look likeDATA1A    12,1A    13,2A    14,4B    21,2B    22,3C    24,4
Adding a new column using information from subheadings
sed;awk
It seems that your output separator is 4 spaces, so that's what this example uses - change it to what ever you need, eg for a TAB, OFS=\t awk 'NR==1 || NF==0 {print; next}  # main heading and blank lines      $1==NAME: {name=$2; next}   # section name/header     {print name, $1}              # data lines    ' OFS=     fileOutput:  DATA1A    12,1A    13,2A    14,4B    21,2B    22,3C    24,4