Sponsored Content
Top Forums Shell Programming and Scripting Insert certain field of matched pattern line above pattern Post 302809463 by jyu3 on Monday 20th of May 2013 12:59:58 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
REGEXP-ASSEMBLE(1p)					User Contributed Perl Documentation				       REGEXP-ASSEMBLE(1p)

NAME
regexp-assemble - Assemble a list of regular expressions from a file SYNOPSIS
regexp-assemble -abcdfinprsStTuUvw file [...] DESCRIPTION
Assemble a list of regular expression either from standard input or a file, using the Regexp::Assemble module. OPTIONS
-a look Ahead. Insert "(?=...)" zero-width lookahead assertions in the pattern, where necessary. -b Blank. Ignore blank lines. -c Comment. Basic comment filtering. Strip off perl/shell comments ("s*#.*$/"). -d Debug. Turns on debugging output. See Regexp::Assemble for suitable values. -i Indent. Print the regular expression using and indent of n to display nesting. A.k.a pretty-printing. Implies -p. -n No newline. Do not print a newline after the pattern. Useful when interpolating the output into a templating system or similar. -p Print. Print the pattern. This is the default, however, it is required when the -t switch is enabled (because if you want to test patterns ordinarily you don't care what the the assembled pattern looks like). -r Reduce. The default behaviour is to reduce the assembled pattern. Enabling this switch causes the reduction algorithm to be switched off. This can help you determine how much reduction is performed. regexp-assemble pattern.file | wc # versus regexp-assemble -r pattern.file | wc -s Statistics. Print some statistics about the assembled pattern. The output is sent to STDERR (in order to allow the generated pattern to be redirected elsewhere). -S Statistics only. Like -s, except that the pattern itself is not output. Useful with -d 8 to see the time taken. -t Test. Test the assembled expression against the contents of a file. Each line is read from the file and is matched against the pattern. Lines that fail to match are printed. In other words, no output is good output. In this mode of operation, error status is 1 in the case of a failure, 0 if all lines matched. -T Time. Print statistics on the time taken to reduce and assemble the pattern. (This is merely a lazy person's synonym for "-d 8"). -u Unique. Carp if duplicate patterns are found. -U Unroll. Transform "a+" et al into "aa*" (which may allow additional reductions). -v Version. Print the version of the regexp-assemble script. -w Word/Whole. When testing the contents of a file with "-t", bracket the expression with "^" and "$" in order to match the whole word or line from the file. DIAGNOSTICS
Will print out a summary of the problem if an added pattern causes the assembly to fail. SEE ALSO
Regexp::Assemble AUTHOR
Copyright (C) 2004-2008 David Landgren. All rights reserved. LICENSE
This script is free software; you can redistribute it and/or modify it under the same terms as Perl itself. perl v5.14.2 2012-06-30 REGEXP-ASSEMBLE(1p)
All times are GMT -4. The time now is 10:23 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy