Identify matching data in a file and output to original line, in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Identify matching data in a file and output to original line, in perl
# 1  
Old 12-04-2008
Identify matching data in a file and output to original line, in perl

Hi,

I haven't done this for awhile, and further, I've never done it in perl so I appreciate any help you can give me.

I have a file of lines, each with 5 data points that look like this:

AB,N,ALLIANCEBERNSTEIN HLDNG L.P,AB,N
ALD,N,ALLIED CAPITAL CORPORATION,ALD,N
AFC,N,ALLIED CAPITAL CORPORATION,ALD,N

Let's call it: a,b,c,d,e

Every time a == d and b == e, I want to then search every other line in the file and find where a == d(newline) and a == e(newline). Whenever this is the case, I want to append the a,b data from the newline to the original line such that it reads: a,b,c,d,e,a(newline),b(newline).

Using the data above, we would output something like this:

AB,N,ALLIANCEBERNSTEIN HLDNG L.P,AB,N
ALD,N,ALLIED CAPITAL CORPORATION,ALD,N,AFC,N
AFC,N,ALLIED CAPITAL CORPORATION,ALD,N

I'm also not sure how many total matches there might be in the file so I could end up with ~100 matching lines for any original line such that the final result for the original line would look like: a,b,c,d,e,a(newline0),b(newline0)...,a(newline100),b(newline100)

Does that make sense?
# 2  
Old 12-05-2008
Read your question and correct /clarify what you want to do. What you say in your question and what you show as the example output don't seem to match.
# 3  
Old 12-05-2008
Sure. Let's go step by step with it. We have: a,b,c,d,e

1. I want to identify all lines of a file where a==d && b==e such that the bold line would be located

ALD,N,ALLIED CAPITAL CORPORATION,ALD,N
AFC,N,ALLIED CAPITAL CORPORATION,ALD,N

2. Next, when this happens, I want to search all other lines and find where a==d(newline) && b==e(newline):

ALD,N,ALLIED CAPITAL CORPORATION,ALD,N
AFC,N,ALLIED CAPITAL CORPORATION,ALD,N

3. When I find one of these lines, I want to append a(newline),b(newline) to the end of the original line such that:

ALD,N,ALLIED CAPITAL CORPORATION,ALD,N,AFC,N

AFC,N,ALLIED CAPITAL CORPORATION,ALD,N

4. There will be multiple instances where a==d(newline) && b==e(newline), and each instance of this will have a unique a(newline), b(newline), such that the final output will look something like:

ALD,N,ALLIED CAPITAL CORPORATION,ALD,N,AFC,N,a(newline0),b(newline0)...,a(newlinen),b(newlinen)
AFC,N,ALLIED CAPITAL CORPORATION,ALD,N

Is that more clear?
# 4  
Old 12-05-2008
Actually, nevermind, I figured it out, which is far more satisfying.
# 5  
Old 12-06-2008
Code:
open FH,"<a.txt";
while(<FH>){
	tr/\n//d;
	@tmp=split(",",$_);
	if(($tmp[0] eq $tmp[3]) && ($tmp[1] eq $tmp[4])){
		$str=$_;
		$str1=$tmp[0];
		$str2=$tmp[1];
		next;
	}
	if(($str1 eq $tmp[3]) && ($str2 eq $tmp[4])){
		$str=$str.",".$tmp[0].",".$tmp[1];
	}
}
close FH;

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 control grep output intact for each matching line?

I have multiple (~80) files (some can be as big as 30GB of >1 billion of lines!) to grep on a pattern, and piped the match to a single file. I have a 96-core machine so that each grep job was sent to the background to speed up the search: file1.tab chr1A_part1 123241847 123241848... (6 Replies)
Discussion started by: yifangt
6 Replies

2. Shell Programming and Scripting

Replace and add line in file with line in another file based on matching string

Hi, I want to achieve something similar to what described in another post: The difference is I want to add the line if the pattern is not found. File 1: A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB ... (11 Replies)
Discussion started by: jyu3
11 Replies

3. Shell Programming and Scripting

Can't Output Piped Perl In-line command to a File

Hello, I'm pretty stumped, and I don't know why I am not able to redirect the output to the 'graphme' file with the command below in Fedora 18. tcpdump -l -n -t "tcp == 18" | perl -ane '($s,$j)=split(/,/,$F,2); print "$s\n";' > graphme In case you're wondering, I was following the example... (2 Replies)
Discussion started by: ConcealedKnight
2 Replies

4. Shell Programming and Scripting

perl script to replace the text in the original file

Hi Folks, I have an html file which contains the below line in the body tagI am trying the replace hello with Hello Giridhar programatically. <body> <P><STRONG><FONT face="comic sans ms,cursive,sans-serif"><EM>Hello</EM></FONT></STRONG></P> </body> I have written the below code to... (3 Replies)
Discussion started by: giridhar276
3 Replies

5. Shell Programming and Scripting

How to find the matching data b/w 2 files in perl?

Hi friends,, i have find the matching data between 2files. My file1 have a data like rs3001336 rs3984736 rs2840532 File2 have a data like rs3736330 1 2359237 A G 0.28 1.099 0.010 rs2840532 1 2359977 G A 0.363 0.3373 1.123 rs3001336 1 ... (4 Replies)
Discussion started by: sureshraj
4 Replies

6. Programming

How to find the matching data b/w 2 files in perl?

Hi friends,, i have find the matching data between 2files. My file1 have a data like rs3001336 rs3984736 rs2840532 File2 have a data like rs3736330 1 2359237 A G 0.28 1.099 0.010 rs2840532 1 2359977 G A 0.363 0.3373 1.123 rs3001336 1 2365193 G A 0.0812 0.07319 1.12 ... (1 Reply)
Discussion started by: sureshraj
1 Replies

7. Shell Programming and Scripting

using perl for matching one file with another file and print into new line

One file is fileA 0.0246*0.0068*0.0013*0.0023*0.0182*0.0028*0.0019*0.4750*0.0028*0.0812*0.0123*0.0018*0.0039*0.0020*0.0028*0.0047*0.0139*0.3330*0.0017*0.0072*0.4789... (4 Replies)
Discussion started by: cdfd123
4 Replies

8. Shell Programming and Scripting

awk help required to group output and print a part of group line and original line

Hi, Need awk help to group and print lines to format the output as shown below INPUT FORMAT set echo on set heading on set spool on /* SCHEMA1 */ CREATE TABLE T1; /* SCHEMA1 */ CREATE TABLE T2; /* SCHEMA1 */ CREATE TABLE T3; /* SCHEMA1 */ CREATE TABLE T4; /* SCHEMA1 */ CREATE TABLE T5;... (5 Replies)
Discussion started by: rajan_san
5 Replies

9. UNIX for Dummies Questions & Answers

How to grep / zgrep to output ONLY the matching filename and line number?

Hi all, I am trying to zgrep / grep list of files so that it displays only the matching filename:line number and does not display the whole line, like: (echo "1.txt";echo "2.txt") | xargs zgrep -no STRING If I use -o option, it displays the matching STRING and if not used, displays the... (3 Replies)
Discussion started by: vvaidyan
3 Replies

10. UNIX for Dummies Questions & Answers

Need to identify the line containing @ in between the line of a file

Hi All, I have a huge unix flat file delimted by @ at the end of the line. I need to find out if there is any line/s containing @ in between the line so that I can remove that and put the file for processing. Thanks in advance for your help. (4 Replies)
Discussion started by: b.paramanatti
4 Replies
Login or Register to Ask a Question