Traverse backwards based on forward pattern match


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Traverse backwards based on forward pattern match
# 1  
Old 01-20-2014
Traverse backwards based on forward pattern match

Hi,

I have a file containing records of this format. I need to show output of all header of the sets containing recn:rvn records. The sets appear in my file like below.

Code:
#set 1
header1:hv1
rec1:rv1
rec2:rv2
rec3:rv3
.......
......
recn:rn

#set 2
header1:hv1
rec1:rv1
rec2:rv2
rec3:rv3
.......
......
recn:rn

#set 3
header1:hv1
rec1:rv1
rec2:rv2
rec3:rv3
.......
......
recn:rn

.......
#set n
......

I need to get output of only those "headern:hvn" which contain "recn:vn" pattern records within the set.

Last edited by Scrutinizer; 01-20-2014 at 07:35 PM.. Reason: code tags
# 2  
Old 01-20-2014
Show your expected output as well.
# 3  
Old 01-20-2014
Thanks Akshay.
The output could be like below assuming set2, set4, set5 dont have the recr:vn(search pattern) records in them.

header1:hv1
header3:hv3
header6:hv6
.....
# 4  
Old 01-21-2014
Quote:
Originally Posted by adurga
Hi,

I have a file containing records of this format. I need to show output of all header of the sets containing recn:rvn records. The sets appear in my file like below.

Code:
#set 1
header1:hv1
rec1:rv1
rec2:rv2
rec3:rv3
.......
......
recn:rn

#set 2
header1:hv1
rec1:rv1
rec2:rv2
rec3:rv3
.......
......
recn:rn

#set 3
header1:hv1
rec1:rv1
rec2:rv2
rec3:rv3
.......
......
recn:rn

.......
#set n
......

I need to get output of only those "headern:hvn" which contain "recn:vn" pattern records within the set.
Quote:
Originally Posted by adurga
Thanks Akshay.
The output could be like below assuming set2, set4, set5 dont have the recr:vn(search pattern) records in them.

header1:hv1
header3:hv3
header6:hv6
.....
Why don't you use code tag ???


if just header try :
Code:
awk '/header/' file

--edit--

Whether you want to substitute
#set 3 numeric value from #set to header1:hv1 ? if so then o/p becomes header3:hv3

Code:
$ awk '/^\#/{gsub(/[[:alpha:]]|[[:space:]]|\#/,x);s=$0}/header/ && gsub(/[[:digit:]]/,s)' file
header1:hv1
header2:hv2
header3:hv3


Last edited by Akshay Hegde; 01-21-2014 at 12:44 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl script to fill the entire row of Excel file with color based on pattern match

Hi All , I have to write one Perl script in which I need to read one pre-existing xls and based on pattern match for one word in some cells of the XLS , I need to fill the entire row with one color of that matched cell and write the content to another excel Please find the below stated... (2 Replies)
Discussion started by: kshitij
2 Replies

2. Shell Programming and Scripting

Find a pattern and traverse left and pick something from another pattern

I have a Text like below , Detailed Table Information Table(tableName:a1, dbName:default, owner:eedc_hdp_s_d-itm-e, createTime:1520514151, lastAccessTime:0, retention:0, sd:StorageDescriptor(cols:, location:hdfs://DBDP-Dev/apps/hive/warehouse/a1,... (6 Replies)
Discussion started by: nv186000
6 Replies

3. Shell Programming and Scripting

awk to update value based on pattern match in another file

In the awk, thanks you @RavinderSingh13, for the help in below, hopefully it is close as I am trying to update the value in $12 of the tab-delimeted file2 with the matching value in $1 of the space delimeted file1. I have added comments for each line as well. Thank you :). awk awk '$12 ==... (10 Replies)
Discussion started by: cmccabe
10 Replies

4. Shell Programming and Scripting

sed to remove newline chars based on pattern mis-match

Greetings Experts, I am in AIX; I have a file generated through awk after processing the input files. Now I need to replace or remove the new-line characters on all lines that doesn't have a ; which is the last character on the line. I tried to use sed 's/\n/ /g' After checking through the... (6 Replies)
Discussion started by: chill3chee
6 Replies

5. Shell Programming and Scripting

Delete lines based on pattern match

BASH in Solaris 10 I have a log file like below. Whenever the pattern ORA-39083 is encountered, I want to delete the line which has this pattern and 3 lines below it. $ cat someLogfile.txt ORA-39083: Object type OBJECT_GRANT failed to create with error: ORA-01917: user or role 'CMPA' does... (4 Replies)
Discussion started by: kraljic
4 Replies

6. Shell Programming and Scripting

Help needed - Split large file into smaller files based on pattern match

Help needed urgently please. I have a large file - a few hundred thousand lines. Sample CP START ACCOUNT 1234556 name 1 CP END ACCOUNT CP START ACCOUNT 2224444 name 1 CP END ACCOUNT CP START ACCOUNT 333344444 name 1 CP END ACCOUNT I need to split this file each time "CP START... (7 Replies)
Discussion started by: frustrated1
7 Replies

7. Shell Programming and Scripting

Help with replace line based on specific pattern match

Input file data20714 7327 7366 detail data20714 7327 7366 main data250821 56532 57634 detail data250821 57527 57634 main data250821 57359 57474 main data250821 57212 57301 main data250821 57140 57159 detail data250821 56834 57082 main data250821 56708 56779 main ... (3 Replies)
Discussion started by: perl_beginner
3 Replies

8. Programming

How to search a file based on a time stamp backwards 10 seconds

Hi all, I'm after some help with this small issue which i'm struggling to work out a fix for. I have a file that contains records that all have a time stamp for each individual record, i need to search the file for a specific time stamp and then search back 10 seconds to see if the number... (2 Replies)
Discussion started by: sp3arsy
2 Replies

9. Shell Programming and Scripting

Concatenating and appending string based on specific pattern match

Input #GEO-1-type-1-fwd-Initial 890 1519 OPKHIJEFVTEFVHIJEFVOPKHIJTOPKEFVHIJTEFVOPKOPKHIJHIJHIJTTOPKHIJHIJEFVEFVOPKHIJOPKHIJOPKEFVEFVOPKHIJHIJEFVHIJHIJEFVTHIJOPKOPKTEFVEFVEFVOPKHIJOPKOPKHIJTTEFVEFVTEFV #GEO-1-type-2-fwd-Terminal 1572 2030... (7 Replies)
Discussion started by: patrick87
7 Replies

10. Shell Programming and Scripting

Merge two file data together based on specific pattern match

My input: File_1: 2000_t g1110.b1 abb.1 2001_t g1111.b1 abb.2 abb.2 g1112.b1 abb.3 2002_t . . File_2: 2000_t Ali england 135 abb.1 Zoe british 150 2001_t Ali england 305 g1111.b1 Lucy russia 126 (6 Replies)
Discussion started by: patrick87
6 Replies
Login or Register to Ask a Question