brianjb has asked for the wisdom of the Perl Monks concerning the following question:
Good afternoon! I am new to this forum. In fact this is my first post. I am querying a database and then trying to join the data onto a single line that has the same id in the first field. Here is an example of what the beginning date looks like:
9885 10.10.9.48 Room 1105A 9885 10.10.9.48 Jack 1105A--05D 9885 10.10.9.48 org_code B703 9885 10.10.9.48 Building 1268A 114948 10.10.184.0 nasa_nets off 114948 10.10.184.0 blockSecName test name 114948 10.10.184.0 blockTechName brian test 114948 10.10.184.0 blockAdminName test admin 114949 10.10.184.0 blockSecName John G. Smooth 114949 10.10.184.0 blockTechPhone 222-555-1212 114949 10.10.184.0 blockAdminName Lucy P. Wallice 114949 10.10.184.0 blockAdminId 8878787 114949 10.10.184.0 block_name unknown 114949 10.10.184.0 blockSecId 787878 114949 10.10.184.0 blockAdminEmail lucy.p.wallice@google.com 114949 10.10.184.0 blockTechName TEST LAN 114949 10.10.184.0 blockSecPhone 222-555-3232 114949 10.10.184.0 blockTechEmail terCInternal@google.com 114949 10.10.184.0 blockSecEmail John.goody@google.com 114949 10.10.184.0 nasa_nets off
Note that the first field has an ID. the second, third, and fourth may be different. I don't need to keep the first field at all. Here is what I would like the end result to look like:
10.10.9.48|Room=1105A|Jack=1105A--05D|org_code=B703|Building=1268A 10.10.184.0|nasa_nets=off|blockSecName=test name|blockTechName=brian t +est|blockA dminName=test admin|blockSecName=John G. Smooth|blockTechPhone=222-555 +-1212|bloc kAdminName=Lucy P. Wallice|blockAdminId=8878787|block_name=unknown|blo +ckSecId=78 7878|blockAdminEmail=lucy.p.wallice@google.com|blockTechName=TEST LAN| +blockSecPhon e=222-555-3232|blockTechEmail=terCInternal@google.com|blockSecEmail=Jo +hn.good y@google.com|nasa_nets=off
Notice that every line that had the same first field was put on the same line. In fact, I suppose we can just ignore the first field and just use the second field. So now that you know I am least trying, here is what I have tried:
$out_file="/tmp/brian_test.$datestring"; open(IN,"<$TMPCONTLIST") or die "Can't open $TMPCONTLIST!\n"; open(OUT,">$out_file") or die "Can't open $out_file!\n"; while ($line=<IN>) { @fields = split /\t/,$line; $line = join "|", @fields[1,2,3,4]; print OUT $line; print OUT "\n"; } close IN; close OUT;
It is giving me this....just a sample
10.10.9.48|User_POC_Phone|222-555-2322 | 10.10.9.48|Room|1105A | 10.10.9.48|Jack|1105A--05D | 10.10.9.48|org_code|B703 | 10.10.9.48|Building|1268A
Can you please help? Thanks in advance!!!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: split then join based on common value in field
by Kenosis (Priest) on Jun 02, 2012 at 02:01 UTC | |
by brianjb (Novice) on Jun 04, 2012 at 20:21 UTC | |
|
Re: split then join based on common value in field
by spazm (Monk) on Jun 02, 2012 at 00:15 UTC |