Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format Question

Status
Not open for further replies.

dbadmin

Programmer
Jan 3, 2003
147
US
Hi Perl Gurus,

I have a file with the following data.

Test1 - Execution1 - 00:00:00.52
Test2 - Execution1 - 00:00:00.01
Test3 - Execution1 - 00:00:00.67
Test4 - Execution1 - 00:01:01.80
Test5 - Execution1 - 00:00:00.31
Test6 - Execution1 - 00:00:00.94
Test7 - Execution1 - 00:00:00.02
Test8 - Execution1 - 00:00:00.07
Test1 - Execution2 - 00:00:00.50
Test2 - Execution2 - 00:00:00.00
Test3 - Execution2 - 00:00:00.58
Test4 - Execution2 - 00:00:59.39
Test5 - Execution2 - 00:00:00.17
Test6 - Execution2 - 00:00:00.74
Test7 - Execution2 - 00:00:00.02
Test8 - Execution2 - 00:00:00.06

I need to format it as follows.

Test Execution1 Execution2
----------------------------------
Test1 00:00:00.52 00:00:00.50
Test2 00:00:00.01 00:00:00.00
Test3 00:00:00.67 00:00:00.58
Test4 00:01:01.80 00:00:59.39
Test5 00:00:00.31 00:00:00.17
Test6 00:00:00.94 00:00:00.74
Test7 00:00:00.02 00:00:00.02
Test8 00:00:00.07 00:00:00.06

I have written one in shell script and is working but thought perl will be a good option. Any help is really appreciated.

Thanks,
dbadmin
 
Try this
proc_file.pl
Code:
[gray][i]# run:[/i][/gray]
[gray][i]# perl proc_file.pl exec_file.txt[/i][/gray]

[url=http://perldoc.perl.org/functions/use.html][black][b]use[/b][/black][/url] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]

[gray][i]# process input and store test execution times in hash[/i][/gray]
[url=http://perldoc.perl.org/functions/my.html][black][b]my[/b][/black][/url] [blue]%test_hash[/blue]=[red]([/red][red])[/red][red];[/red]
[olive][b]while[/b][/olive] [red]([/red]<STDIN>[red])[/red][red]{[/red]
  [url=http://perldoc.perl.org/functions/chomp.html][black][b]chomp[/b][/black][/url][red];[/red]
  [black][b]my[/b][/black] [blue]@exec_time_list[/blue] = [red]([/red][red])[/red][red];[/red]
  [black][b]my[/b][/black] [red]([/red][blue]$test_id[/blue], [blue]$exec[/blue], [blue]$exec_time[/blue][red])[/red] = [url=http://perldoc.perl.org/functions/split.html][black][b]split[/b][/black][/url][red]([/red][red]/[/red][purple][purple][b]\s[/b][/purple]*-[purple][b]\s[/b][/purple]*[/purple][red]/[/red], [blue]$_[/blue][red])[/red][red];[/red]
  [gray][i]#print "\$test_id = '$test_id', \$exec_time = '$exec_time'\n";[/i][/gray]
  [olive][b]if[/b][/olive] [red]([/red]not [blue]$test_hash[/blue][red]{[/red][blue]$test_id[/blue][red]}[/red][red])[/red][red]{[/red]
    [gray][i]# store execution time into list[/i][/gray]
    [url=http://perldoc.perl.org/functions/push.html][black][b]push[/b][/black][/url] [red]([/red][blue]@exec_time_list[/blue], [blue]$exec_time[/blue][red])[/red][red];[/red]
  [red]}[/red] 
  [olive][b]else[/b][/olive] [red]{[/red]
    [gray][i]# dereference hash element list[/i][/gray]
    [blue]@exec_time_list[/blue] = [blue]@[/blue][red]{[/red][blue]$test_hash[/blue][red]{[/red][blue]$test_id[/blue][red]}[/red][red]}[/red][red];[/red]
    [gray][i]# push next time to the list[/i][/gray]
    [black][b]push[/b][/black] [red]([/red][blue]@exec_time_list[/blue], [blue]$exec_time[/blue][red])[/red][red];[/red]
  [red]}[/red]  
  [gray][i]# add list reference to hash[/i][/gray]
  [blue]$test_hash[/blue][red]{[/red][blue]$test_id[/blue][red]}[/red] =\[blue]@exec_time_list[/blue][red];[/red]
[red]}[/red]
[gray][i]# Print out results[/i][/gray]
[url=http://perldoc.perl.org/functions/printf.html][black][b]printf[/b][/black][/url][red]([/red][red]"[/red][purple]%-6s  %-12s  %-12s\n[/purple][red]"[/red], [red]'[/red][purple]Test[/purple][red]'[/red], [red]'[/red][purple]Execution1[/purple][red]'[/red], [red]'[/red][purple]Execution2[/purple][red]'[/red][red])[/red][red];[/red]
[olive][b]foreach[/b][/olive] [black][b]my[/b][/black] [blue]$key[/blue] [red]([/red][url=http://perldoc.perl.org/functions/sort.html][black][b]sort[/b][/black][/url] [url=http://perldoc.perl.org/functions/keys.html][black][b]keys[/b][/black][/url] [red]([/red][blue]%test_hash[/blue][red])[/red][red])[/red][red]{[/red]
  [gray][i]# dereference list  [/i][/gray]
  [black][b]my[/b][/black] [blue]@exec_time_list[/blue] = [blue]@[/blue][red]{[/red][blue]$test_hash[/blue][red]{[/red][blue]$key[/blue][red]}[/red][red]}[/red][red];[/red]
  [gray][i]# create list of data to print[/i][/gray]
  [black][b]push[/b][/black][red]([/red][black][b]my[/b][/black] [blue]@report_list[/blue], [blue]$key[/blue], [black][b]sort[/b][/black] [blue]@exec_time_list[/blue][red])[/red][red];[/red]
  [gray][i]#print "@report_list\n";[/i][/gray]
  [black][b]printf[/b][/black][red]([/red][red]"[/red][purple]%-6s  %-12s  %-12s\n[/purple][red]"[/red], [blue]@report_list[/blue][red])[/red] 
[red]}[/red]
[tt]------------------------------------------------------------
Pragmas (perl 5.10.0) used :
[ul]
[li]strict - Perl pragma to restrict unsafe constructs[/li]
[li]warnings - Perl pragma to control optional warnings[/li]
[/ul]
[/tt]
Output:
Code:
C:\Users\Roman\Work>perl proc_file.pl < exec_file.txt
Test    Execution1    Execution2
Test1   00:00:00.50   00:00:00.52
Test2   00:00:00.00   00:00:00.01
Test3   00:00:00.58   00:00:00.67
Test4   00:00:59.39   00:01:01.80
Test5   00:00:00.17   00:00:00.31
Test6   00:00:00.74   00:00:00.94
Test7   00:00:00.02   00:00:00.02
Test8   00:00:00.06   00:00:00.07
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top