Insert certain field of matched pattern line above pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert certain field of matched pattern line above pattern
# 1  
Old 05-20-2013
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
Code:
> 
# @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
Code:
> 
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
Code:
awk -v pat="# @D" '{a[NR]=$0;}END{for(i=1;i<=NR;i++){if(a[i] ~ pat ){print a[i]"\n" a[i];}else{print a[i];}}}' 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
# 2  
Old 05-20-2013
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
# 3  
Old 05-20-2013
Thanks for the quick reply.
Do you mean:
Code:
awk -F \| -v pat="# @D" '{a[NR]=$0;}END{for(i=1;i<=NR;i++){if(a[i] ~ pat ){print $5"\n" a[i];}else{print a[i];}}}' tst_geo.gmt >output.txt

I tried that but it doesn't work the way I expected
# 4  
Old 05-20-2013
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.
Code:
/# @D/ {print $5} {print}

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 5  
Old 05-20-2013
You are right!
Thank you very much for the help.
I was making things too complicated.....

For the record
Code:
awk -F \| '/# @D/ {print $5} {print}' tst_geo.gmt > ttt.txt

This User Gave Thanks to jyu3 For This Post:
# 6  
Old 05-20-2013
You're quite welcome.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print previous line of multiple pattern matched line?

Hello, I have below format log file, Comparing csv_converted_files/2201/9747.1012H67126.5077292103609547345.csv and csv_converted_files/22019/97447.1012H67126.5077292103609547345.csv Comparing csv_converted_files/2559/9447.1012H67126.5077292103609547345.csv and... (6 Replies)
Discussion started by: arvindshukla81
6 Replies

2. UNIX for Advanced & Expert Users

To print from the first line until pattern is matched

Hi I want to print the line until pattern is matched. I am using below code: sed -n '1,/pattern / p' file It is working fine for me , but its not working for exact match. sed -n '1,/^LAC$/ p' file Input: LACC FEGHRA 0 LACC FACAF 0 LACC DARA 0 LACC TALAC 0 LAC ILACTC 0... (8 Replies)
Discussion started by: Abhisrajput
8 Replies

3. Shell Programming and Scripting

Print line between two patterns when a certain pattern matched

Hello Friends, I need to print lines in between two string when a keyword existed in those lines (keywords like exception, error, failed, not started etc). for example, input: .. Begin Edr ab12 ac13 ad14 bc23 exception occured bd24 cd34 dd44 ee55 ff66 End Edr (2 Replies)
Discussion started by: EAGL€
2 Replies

4. Shell Programming and Scripting

How to use sed to search a particular pattern in a file backward after a pattern is matched.?

Hi, I have two files file1.txt and file2.txt. Please see the attachments. In file2.txt (which actually is a diff output between two versions of file1.txt.), I extract the pattern corresponding to 1172c1172. Now ,In file1.txt I have to search for this pattern 1172c1172 and if found, I have to... (9 Replies)
Discussion started by: saurabh kumar
9 Replies

5. Shell Programming and Scripting

Insert new pattern in newline after the nth occurrence of a line pattern - Bash in Ubuntu 12.04

Hi, I am getting crazy after days on looking at it: Bash in Ubuntu 12.04.1 I want to do this: pattern="system /path1/file1 file1" new_pattern=" data /path2/file2 file2" file to edit: data.db - I need to search in the file data.db for the nth occurrence of pattern - pattern must... (14 Replies)
Discussion started by: Phil3759
14 Replies

6. Shell Programming and Scripting

how do I break line in a file when a pattern is matched ?

Hi All, I am stuck for quite sometime now. Below is a line in my file - GS|ED|001075|001081|20110626|1806|100803|X|004010ST|130|100803001 This line occurs only once and it is the second line. I have to break this line into two lines from ST (bold) such that it looks like -... (5 Replies)
Discussion started by: ihussain
5 Replies

7. Shell Programming and Scripting

Help required on joining one line above & below to the pattern matched string line.

Hi Experts, Help needed on joining one line above & below to the pattern matched string line. The input file, required output is mentioned below Input file ABCD DEFG5 42.0.1-63.38.31 KKKK iokl IP Connection Available ABCD DEFG5 42.0.1-63.38.31 ... (7 Replies)
Discussion started by: krao
7 Replies

8. Shell Programming and Scripting

awk script to move a line after the matched pattern line

I have the following text format in a file which lists the question first and then 5 choices after that the explanantion and finally the answer. 1.The amount of time it takes for most of a worker’s occupational knowledge and skills to become obsolete has been declining because of the... (2 Replies)
Discussion started by: nanchil_guy
2 Replies

9. Shell Programming and Scripting

extracting matched pattern from a line using sed

I am trying to pull certain pieces of data out of a line of a file that matches a certain pattern: The three pieces that I want to pull out of this line are the only occurrences of that pattern within the line, but the rest of the line is not consistent in each file. Basically the line is... (3 Replies)
Discussion started by: ellhef
3 Replies

10. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies
Login or Register to Ask a Question