如何读取文件并用名称替换值

时间:2010-08-21 05:20:15

标签: perl

我有一个文件,我将称之为源ID。我想读取该文件,并根据源ID为其添加名称,例如15431 = MyTV8。我有一个列出源ID和名称的文件,但我不知道如何读取原始ID并将通道映射文件中的名称添加到包含源ID的文件中。

我调查的源ID的一小部分样本

13431
0
4112
4129
11412
0
0
12055
0
0
4129
0
0
12125
12120
0
10959
5370
5380
4109
0
20121
0
12334
11493
0
0
4107
5687
0
4008
0
0
4166
5378
4203
0
12056
0
0
0
0
4286
4183
0
0
4129
4037
0
10555
12209
11205
8846
0
0
5380
4719
0
4166
4117
4150
0
4183
0
4123
10391
0
4183
4151
5531
4107
4179
4185
4203
4203
0
4107

它们只是在文本文件中,这是源ID信道映射

1052 SCIFI                  
1053 COMEDY                  
1066 CSPAN2                  
1067 ESPN CLASSIC            
1100 SUNSHINE FOOTBALL       
1133 TRUTV                   
1241 SHOWTIME 2 EAST         
1312 MC METAL                
1315 MC KIDS ONLY            
1318 MC HIP HOP & R&B        
1319 MC MC MIX TAPE          
1324 MC LITE CLASSICAL       
1326 MC SINGERS & SWING      
1330 MC R&B CLASSICS         
1332 MC MEXICANA             
1334 MC RAGGAE               
1338 MC HIP HOP CLASSICS     
1339 MC SOUND OF SEASON      
1340 MC HIT LIST             
1346 MC PARTY FAVORITES      
1348 MC POP HITS             
2006 COLLEGE SPORTS TV       
2469 WGCU WORLD              
2470 WGCU ED TV              
3014 ACTION MAX WEST         
3048 SHOWTIME SHOWCASE 3     
3051 STARZ! 1 WEST           
3059 JEWELRY TV              
3064 STARZ! IN BLACK         
3068 WAM.                    
3069 STARZ! 5 CINEMA WEST    
3093 NICK TOO                
4003 ACTION WEST             
4008 NOGGIN                  
4011 ENCORE DRAMA EAST       
4029 LOGO MTV                
4037 MOVIEPLEX               
4068 FCS SOCC                
4070 EWTN DIGITAL            
4081 TELEFUTURA              
4100 ADS-AMC                 
4101 ADS-A&E                 
4103 ADS-BET                 
4104 ADS-BRVO                
4105 ADS-C-SPAN              
4107 ADS-TOON                
4108 ADS-CMT                 
4109 ADS-CNBC                
4110 ADS-CNN                 
4112 ADS-CMDY                
4115 ADS-TRUTV/COURT-TV      
4117 ADS-E!                  
4119 ADS-ESPN                
4120 ADS-ESPN2               
4121 ADS-EWTN                
4123 ADS-HALLMARK            
4124 FIT TV                  
4125 ADS-FX                  
4127 ADS-CCTV                
4128 ADS-HIST                
4129 ADS-HGTV                
4130 ADS-HSN                 
4150 ADS-MSNBC               
4151 ADS-MTV                 
4156 ADS-NICK                
4157 BARKER                  
4159 PLAYBOY DIR. CUT /CP    
4163 ADS-STYLE               
4164 ADS-QVC                 
4166 ADS-SCIFI               
4178 ADS-DSC                 
4179 ADS-FAM                 
4181 ADS-TLC                 
4182 ADS-SPIKE               
4183 ADS-USA                 
4184 ADS-TWC-DIGITAL         
4185 ADS-TNT                 
4186 ADS-FOOD                
4187 ADS-VH-1                
4203 ADS-FXNEW               
4241 ADS-TRAVL               
4286 ADS-TCM                 
4460 GAME SHOW               
4485 MTV2.                   
4545 SUNDANCE EAST           
4557 ADS-TVL                 
4702 ADS-VERSUS              
4705 ADS-SPEED               
4719 ADS-GOLF                
4730 ADS-WGN                 
5012 ADS TEST 1              
5013 ADS TEST 2              
5014 ADS TEST 3              
5015 ADS TEST 4              
5016 ADS TEST 5              
5017 ADS TEST 6              
5018 ADS TEST 7              
5019 ADS TEST 8              
5020 ADS TEST 9              
5021 ADS TEST 10             
5022 ADS TEST 11             
5023 ADS TEST 12             
5024 SUN SPORTS HD           
5286 SHOP NBC.               
5368 ADS-WEDU                
5369 ADS-WXPX                
5370 ADS-WFLA                
5377 ADS-WTOG                
5378 ADS-WTSP                
6031 ADS-WRXY                
6166 ADS-WWSB                
6206 ADS-WZVN                
6884 DXD / TOON DISNEY       
7064 MY38 WB HD              
7077 BLOOMBERG TV            
7085 BIG TEN                 
7086 BIG TEN 2               
7090 ADS-HALLMARK VS         
7331 CURRENT TV              
7795 G4.                     
8282 BIOGRAPHY               
8285 HISTORY INTER.          
8434 ADS-WTTA                
8445 OUTDOOR CH              
8499 OXYGEN                  
8675 DO IT YOURSELF          
8841 WEATHERSCAN             
8846 LIFETIME MOVIE NET.     
8888 BRAVO                   
9144 NHL & MLB GAME 1        
9145 NHL & MLB GAME 2        
9145 NHL & MLB GAME 2        
9145 NHL & MLB GAME 2        
9146 NHL & MLB GAME 3        
9146 NHL & MLB GAME 3        
9147 NHL & MLB GAME 4        
9147 NHL & MLB GAME 4        
9147 NHL & MLB GAME 4        
9148 NHL & MLB GAME 5        
9148 NHL & MLB GAME 5        
9148 NHL & MLB GAME 5        
9149 NHL & MLB GAME 6        
9149 NHL & MLB GAME 6        
9149 NHL & MLB GAME 6        
9150 NHL & MLB GAME 7        
9150 NHL & MLB GAME 7        
9150 NHL & MLB GAME 7        
9151 NHL & MLB GAME 8        
9151 NHL & MLB GAME 8        
9151 NHL & MLB GAME 8        
9152 NHL & MLB GAME 9        
9152 NHL & MLB GAME 9        
9152 NHL & MLB GAME 9        
9153 NHL & MLB GAME 10       
9153 NHL & MLB GAME 10       
9201 FUSE                    
9237 THEN / GAS              
9472 C-SPAN3                 
9544 TV GAMES                
9550 SOAPNET                 
9597 GAC                     
10000 CINEMAX                 
10001 ENCORE EAST             
10002 HBO-2                   
10004 HBO-3 SIGNATURE         
10005 SHOWTIME EAST           
10006 STARZ! 1 EAST           
10008 DISNEY CHANNEL.         
10010 TMC                     
10011 SHOWTIME EXTREME E      
10012 HBO WEST                
10014 CINEMAX WEST            
10021 ADS-ANPL                
10023 CINEMAX MORE MAX -2     
10025 TMC XTRA 2 EAST         
10026 ACTION MAX EAST         
10028 THRILLER MAX EAST       
10041 NICKTOONS               
10042 MTV JAMS                
10045 VH-1 SOUL               
10046 CMT PURE                
10057 OUTERMAX EAST           
10063 STARZ! 4 KIDS FAMILY    
10065 STARZ! 5 CINEMA EAST    
10067 VH-1 CLASSIC            
10098 HBO LATINO E            
10389 WORD                    
10391 NATIONAL GEOGRAPHIC     
10398 DAYSTAR TV              
10475 SHOWTIME FAMILY         
10555 @MAX EAST               
10556 WOMANMAX                
10558 5STARMAX EAST           
10584 FCS ATL                 
10585 FCS CEN                 
10586 FCS PAC                 
10911 SHOWTIME HDTV           
10935 THEATER HD CMCHD4       
10959 MTV HITS                
10962 FINE LIVING/COOK TV     
11101 ADS-CW6/WTVK46          
11126 ADS-PAX / ION           
11132 ADS-WMOR                
11205 ADS-SNN                 
11242 RFD-TV                  
11299 ADS-ESPN HD             
11320 ADS-LIFE                
11324 ADS-FSN-FLORIDA         
11328 ADS-CC-SPORT-S          
11406 ADS-DHC                 
11411 ADS-SUN                 
11412 ADS-CNN-HDLN            
11414 ESPNU                   
11431 NBA TV                  
11446 SPORTSMAN               
11493 TENNIS TV               
11514 ESPN SPPV1              
11515 ESPN SPPV2              
11516 ESPN SPPV3              
11517 ESPN SPPV4              
11518 ESPN SPPV5              
11519 ESPN SPPV6              
11647 GOL TV                  
11684 UNIVERSAL HD CMCHD2     
11714 STARZ COMEDY            
11716 STARZ HD                
11855 TV ONE.                 
11911 ADS-TNTHD               
12002 CENTRIC                 
12016 HBO EAST                
12020 IFC                     
12023 LOVE STORIES E.         
12027 MYSTERY EAST            
12030 OVATION                 
12032 SHOWTIME 2 EAST         
12037 STARZ! EDGE             
12044 WESTERNS EAST           
12045 ACTION EAST             
12049 DISCOVERY KIDS          
12050 PLANET GREEN            
12051 DISCOVERY SCIENCE       
12055 WE.                     
12056 MILITARY CHANNEL.       
12112 ESPNEWS                 
12120 DISC ID                 
12125 NFL NETWORK             
12130 FLIX EAST               
12174 SHOWTIME BEYOND EAST    
12175 BBC AMERICA             
12209 ADS-TBS                 
12306 ADS-REALTY-TV-PC        
12333 ADS-LOPC                
12334 ADS-ACCESS-CH-19        
12336 ADS-SARASOTA-ED         
12339 ADS-LO                  
12340 ADS-TV-GUIDE            
12393 WXPXHD                  
12800 ADS-ESPN2HD             
12810 WFTX FOX HD             
12817 ADS-TBS HD              
13008 HBO ZONE EAST           
13011 HBO FAMILY EAST         
13013 HBO COMEDY EAST         
13017 WZVN PC HDTV            
13039 WBBH PC HDTV            
13050 NAT GEO WILD            
13072 HALLMARK MOVIE CH       
13104 GOSPEL MUSIC TV         
13135 HBO HD CMCHD5           
13136 MAX HD CMCHD5           
13229 SPROUT "E" NETWORKS     
13268 WFLA NBC HD             
13289 WTVT FOX HD             
13406 EMPLOYEE CHANNEL        
13430 INDIEPLEX               
13431 RETROPLEX               
13467 ADS-SCI-FI HD           
13551 PALADIEA HD             
13738 HGTV HD CMCHD1          
13745 NATGEO HD CMCHD3        
13907 TV FOOD HD CMCHD2       
14102 NFLHD                   
14507 ADS-WNFM-8              
14710 A&E HD CMCHD1           
15000 IN DEMAND 1             
15001 IN DEMAND 2             
15045 SHORTEEZ ADULT          
15067 NBA TEAM 1              
15068 NBA TEAM 2              
15069 NBA TEAM 3              
15070 NBA TEAM 4              
15071 NBA TEAM 5              
15072 NBA TEAM 6              
15073 NBA TEAM 7              
15074 NBA TEAM 8              
15075 NBA TEAM 9              
15076 NBA TEAM 10             
15128 REELZ                   
15201 VERSUS - HD             
15238 NHL & MLB GAME 14       
15239 NHL & MLB GAME 13       
15240 NHL & MLB GAME 12       
15241 NHL & MLB GAME 11       
15364 MTV SPANISH TR3         
15542 PENTHOUSE/TEN ADULT     
16116 ADS-FOX BUSINESS        
16120 ANIMAL HD CMCHD4        
16121 SCIENCE HD CMCHD8       
16122 TDC HD CMCHD3           
16123 TLC HD CMCHD5           
16129 USA HD CMCHD3           
16141 CNN HD CMCHD7           
16240 LIFETIME MOVIENET HD    
16280 NHL NETWORK             
16317 RETIREMENT TV           
16346 FX HD CMCHD9            
16403 GOLF HD CMCHD7          
16414 AMC HD CMCHD7           
16430 BIOGRAPHY HD            
16543 SPEED HD CMCHD9         
16547 SPIKE HD                
16686 DISNEY HD CMCHD-8       
16692 FAMILY HD CMCHD8        
16713 NICKELODEON HD          
16756 FOX NEWS HD CMCHD9      
16788 WEDU HD                 
16792 WTSP DIGITAL WEATHER    
16793 WTSP HDTV               
16798 LIFETIME HD             
16807 WWSB HDTV               
16817 PLANET GREEN HD         
17000 FRESH ADULT             
17512 MLB NETWORK             
17535 MLBHD                   
18030 WGCU HD PBS             
18324 HEADLINE NEWS HD        
18333 TCM HD                  
18411 TRU TV HD               
18630 FOX SPORTSNET FL HD     
19005 3DTV 1                  
19421 3DTV 2                  
20101 MC 90'S                 
20102 MC ADULT ALTERNATIVE    
20104 MC RAP                  
20105 MC THROWBACK JAMZ       
20106 MC CONTEMP CHRISTIAN    
20107 MC CLASSIC COUNTRY      
20108 MC TODAY'S COUNTRY      
20110 MC ROCK                 
20112 MC GOSPEL               
20113 MC RETRO ROCK           
20115 MC TODDLER TUNES        
20116 MC SOFT ROCK            
20119 MC SOUNDSCAPES          
20120 MC JAZZ                 
20121 MC SMOOTH JAZZ          
20122 MC CLASSICAL MASTERP    
20124 MC 80'S                 
20126 MC EASY LISTENING       
20127 MC POP LATINO           
20128 MC SOLID GOLD OLDIES    
20129 MC R&B SOUL             
20131 MC TRUE COUNTRY         
20282 MC STAGE & SCREEN       
20284 MC MUSICA URBANA        
20287 MC TROPICALS            
20289 MC CLASSIC ROCK         
20338 MC CLASSIC ALTERNATV    
20339 MC DANCE ELECTRONICA    
20357 MC ALTERNATIVE          
20358 MC ROMANCES             
20359 MC BLUES                
20360 MC 70'S                 
21170 STARZ! HD CMCHD1        
21203 SCI FI HD CMCHD2        
21442 THC HD CMCHD4           
23104 E! HD                   
23105 TRAVEL HD               
23106 CARTOON HD              
23107 CSS HD.                 
23109 NFL RED ZONE            
23110 IND HDPPV               
23111 IND HDPPV2              
23112 ESPNU HD

2 个答案:

答案 0 :(得分:4)

这里有一个通用的模式,就是这样:这是一个集交集问题,因此可以使用Perl“sets”轻松解决,我们倾向于称之为哈希:)。

方法是创建一个数据文件的集合(即将其索引到哈希表中,如下所示:%set = (a => 1, b => 1, c => 1),其中实际值并不重要,因为我们将会是使用“设置成员资格运算符”exists)进行测试。

完成后,只需要浏览其他数据集,并在上面的%set中测试其中一个密钥(在您的情况下,源ID)的成员资格。

因此,假设频道映射文件采用以下格式:

ID,NAME

并且源ID文件每行只有一个ID,您可以执行以下操作:

open my $CHANNEL_MAP, q{<}, $channel_map_file_name or die $!;
my %channel_map
    = map {
        chomp;
        my @mapping = split /,/;
        $mapping[0] => $mapping[1] # not needed, just being explicit.
      } <$CHANNEL_MAP>;
close $CHANNEL_MAP or die $!;

# source ID file passed as a command line argument
while (<>) {
    chomp;
    print "$_ = $channel_map{$_}\n"
        if exists $channel_map{$_};
}

然而,这似乎有点无意义,因为您似乎想要做的是通过现有的源ID过滤频道映射文件。在这种情况下,您可以简单地索引源ID文件(使用上面的技术),然后只需浏览通道映射文件并打印源ID中存在ID的每一行。

答案 1 :(得分:0)

由于你不清楚文件的格式,所以这一切都很模糊。

但是,假设您有一个名为names.txt的文件,列出了“15431 = MyTV8”格式的名称,每行一个,而另一个文件只列出数字,您可以使用以下内容:< / p>

#!/usr/bin/perl

open NAMES, '< names.txt' or die $@;
my %names = map { /(\d+) = (.+)/; ($1, $2) } (<NAMES>);

while (<>)
{
   chomp;
   if (exists $names{$_}) {
      print "$_ = $names{$_}\n";
   } else {
      print "$_\n";
   }
}