awk or sed to print the character from the previous line after the regexp match

Hi All,

I need to print the characters in the previous line just before the regular expression match
Please have a look at the input file as attached
I need to match the regular expression ^ with the character of the previous like and also the pin numbers
and the output file should be like this as attached
I am writing the script as stated below
sed -n '/^/{x;p;d;}; x' input_file but how to match the Pin number and character before the regular expression in a single line

Hi, could you please post the input and output sample as code segments and not as attachments?

First off: if you want to match a circumflex, you need to escape with a backslash, since it is a special character in regex:

/\^/

Thanks but I am not able to put the same content in the text format since the lines are not getting aligned.
Thats why I have attached the attachments

--- Post updated at 08:46 PM ---

Input file

                                       p p p p p
Pin Numbers                             3 2 1 8 9
                                        1 2 3 4 5

pattern     offset                        
scan1        2965                       H L H L H
                                            ^
scan2        2200                       L H H L H 
                                          ^
scan3        1100                       H L L L L 
                                          ^
scan4        1500                       L L H H H 
                                            ^
scan5        2800                       H H L H H
                                          ^     ^

Please have a look at the attachment

Your post headings are somewhat contradictory. If happy with awk and not insist on TCL , try

awk '
NR < 4          {if (NR == 1) ST = index ($0, "p")
                 for (i=ST; i<=NF; i+=2) P = P $i
                 next
                }
!NF             {next
                }

/patt/          {print substr ($0, 1, ST-1), "pin-number expected"
                 next
                }

                {getline TMP
                 PTR = 0
                 while (IX = index (TMP, "^"))  {print substr ($0, 1, ST-1), P[IX+PTR], substr ($0, IX+PTR, 1)
                                                 PTR = IX
                                                 TMP = substr (TMP, PTR+1)
                                                }
                }
' FS="" file
pattern     offset                       pin-number expected
scan1        2965                        p13 H
scan2        2200                        p22 H
scan3        1100                        p22 L
scan4        1500                        p13 H
scan5        2800                        p22 H
scan5        2800                        p95 H
1 Like

Thanks a lot Rudic , Let me try it out

Hi Rudic ,

Thanks it worked , I am trying out same script for one more input file


                              r                                     s       
                              e                                     y       
Pin Numbers                   s                                     n       
                              e p p p p p p p p p p p p p p p p p p c       
                              t a a a a a a a a a a a a a a a a a a h       
                              _ 3 3 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 r       
                              n 0 1 0 1 4 5 6 7 8 9 8 9 2 3 4 5 6 7 o       


pattern         offset   
scan0_core_p    2965          1 1 1 0 1 1 0 1 0 1 0 L L L L L H L L 0       
                                                              ^             
scan0_core_p    2967          1 1 1 1 1 0 1 0 1 1 1 H H L L L H L L 0       
                                                              ^             
scan0_core_p    2968          1 1 1 1 0 1 1 0 0 1 1 H L L L H H H L 0       
                                                              ^             
scan0_core_p    2976          1 1 1 1 0 1 1 1 1 0 0 L L H L L H H L 0       
                                                                  ^         
scan0_core_p    2977          1 1 1 1 1 0 1 1 1 1 0 L H H L L H L L 0       
                                                                  ^         
scan0_core_p    2978          1 1 1 0 0 0 1 0 1 1 0 H H L L L H L L 0       
                                                                  ^      

I am changing the script so that it can work for the above stated input file
I am changing some condition in for loop , Any suggestions ?

What did you try? What did you change? Where and how did it fail?