Insert certain field of matched pattern line above pattern

Hello every,

I am stuck in a problem. I have file like this. I want to add the fifth field of the match pattern line above the lines starting with "# @D". The delimiter is "|"
eg

> 
# @D0.00016870300|0.05501020000|12876|12934|3||Qp||Pleistocene||"3 Qp    Pleistocene"|Q
# @P
-82.153320080911939 24.544923921489307
-82.157188580915545 24.547376421491592
-82.161033580919124 24.550294921494309
-82.161888080919908 24.554044921497802
-82.161247080919324 24.557744921501246
-82.159134080917354 24.561397421504651
-82.156906080915277 24.56450292150754
-82.156524580914919 24.565032921508035
-82.152671580911331 24.562114921505319
-82.151329080910074 24.558347921501809
-82.152969580911616 24.55421442149796
-82.149078580907997 24.552226921496107
-82.148239080907203 24.548477421492617
-82.149360580908251 24.544790421489182
-82.153320080911939 24.544923921489307
> 
# @D0.00123462000|0.23518800000|12875|12933|12||Qp||Pleistocene||"3 Qp    Pleistocene"|Q
# @P
-81.750862080537118 24.558242921501712
-81.754951580540933 24.55513742149882
-81.758926580544625 24.554820921498525
-81.764396580549715 24.554092421497849
-81.768371580553435 24.553773921497552
-81.776359580560865 24.552207921496091
-81.780334580564571 24.551889421495794
-81.784843580568776 24.550657421494648

Expected output

> 
3
# @D0.00016870300|0.05501020000|12876|12934|3||Qp||Pleistocene||"3 Qp    Pleistocene"|Q
# @P
-82.153320080911939 24.544923921489307
-82.157188580915545 24.547376421491592
-82.161033580919124 24.550294921494309
-82.161888080919908 24.554044921497802
-82.161247080919324 24.557744921501246
-82.159134080917354 24.561397421504651
-82.156906080915277 24.56450292150754
-82.156524580914919 24.565032921508035
-82.152671580911331 24.562114921505319
-82.151329080910074 24.558347921501809
-82.152969580911616 24.55421442149796
-82.149078580907997 24.552226921496107
-82.148239080907203 24.548477421492617
-82.149360580908251 24.544790421489182
-82.153320080911939 24.544923921489307
> 
12
# @D0.00123462000|0.23518800000|12875|12933|12||Qp||Pleistocene||"3 Qp    Pleistocene"|Q
# @P
-81.750862080537118 24.558242921501712
-81.754951580540933 24.55513742149882
-81.758926580544625 24.554820921498525
-81.764396580549715 24.554092421497849
-81.768371580553435 24.553773921497552
-81.776359580560865 24.552207921496091
-81.780334580564571 24.551889421495794
-81.784843580568776 24.550657421494648

I tried something like this

awk -v pat="# @D" '{a[NR]=$0;}END{for(i=1;i<=NR;i++){if(a ~ pat ){print a"\n" a;}else{print a;}}}' tst_geo.gmt >output.txt

I feel like I am almost there, but just can't figure out how to print certain filed of the matched line.
Please help

Set the field separator to the pipe symbol (-F\|) and print $5 whenever a line matching your pattern, /# @D/, is encountered.

Regards and welcome to the forum,
Alister

Thanks for the quick reply.
Do you mean:

awk -F \| -v pat="# @D" '{a[NR]=$0;}END{for(i=1;i<=NR;i++){if(a ~ pat ){print $5"\n" a;}else{print a;}}}' tst_geo.gmt >output.txt

I tried that but it doesn't work the way I expected

No, that's not what I meant. What I described in my first post is all you need to do. There is no need to store the file in an array.

Throw away what you've got. Then, simply set the separator, see if the line matches the pattern, if it does print the fifth field. Then print the entire record.

/# @D/ {print $5} {print}

Regards,
Alister

1 Like

You are right!
Thank you very much for the help.
I was making things too complicated.....

For the record

awk -F \| '/# @D/ {print $5} {print}' tst_geo.gmt > ttt.txt
1 Like

You're quite welcome.

Regards,
Alister