Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2014
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. #/media/yiguanas/Sam3/ubuntuSW/MT4/tester/history/AJnicify.pl
  4.  
  5. #INPUT:
  6. #USDJPY.txt: (is the original, SORTED file as extracted from the zip archive)
  7. #<TICKER>,<DTYYYYMMDD>,<TIME>,<OPEN>,<HIGH>,<LOW>,<CLOSE>,<VOL>
  8. #USDJPY,20010102,230300,114.43,114.43,114.43,114.43,4
  9. #USDJPY,20010102,230400,114.44,114.44,114.44,114.44,4
  10. #USDJPY,20010102,230500,114.44,114.44,114.44,114.44,4
  11.  
  12. #OUTPUT format: remove header, symbol and comma; add hyphens and colon; remove 00
  13. #2001-11-09,20:19,120.29,120.30,120.29,120.30,4
  14. #2001-11-09,20:20,120.30,120.30,120.30,120.30,4
  15. #2001-11-09,20:21,120.30,120.31,120.30,120.31,4
  16. #2001-11-09,20:22,120.31,120.31,120.31,120.31,4
  17. #2001-11-09,20:23,120.31,120.31,120.30,120.30,4
  18.  
  19. #TO EXECUTE THIS PROGRAM:
  20. # $ cat ./USDJPY.txt | perl ./AJnicify.pl > xxx.txt #OR:
  21. # $ cat ./USDJPY.txt | perl ./AJnicify.pl 20011109,2019 20011109,2022
  22. #2001-11-09,20:20,120.30,120.30,120.30,120.30,4
  23. #2001-11-09,20:21,120.30,120.31,120.30,120.31,4
  24. #2001-11-09,20:22,120.31,120.31,120.31,120.31,4
  25.  
  26. #XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  27.  
  28. sub no_args_sub {
  29.  
  30. <>; #first line (=unwanted header) gets lost intentionally
  31.  
  32. while ( defined($_ = <>) ) #reads from stdin (& theoretically also command line args)
  33. {
  34. # s/USDJPY,//; #works
  35. s/([A-Z].....,)(.*)/2/; #remove first 6 capital letters
  36. s/([0-9]...)([0-9].)([0-9].),([0-9].)([0-9].)([0-9].)/1-2-3,4:5/;
  37. print $_;
  38. } #while
  39.  
  40. } #sub no_args_sub
  41.  
  42. #XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
  43.  
  44. sub two_args_sub {
  45. open STDERR, '>/dev/null'; #naughty!
  46.  
  47. #my distinction of stdin / command line args isn't perfect. Want to avoid error msgs:
  48. #Can't open 20011109,2019: No such file or directory at ./AJnicify.pl line 69.
  49. #Can't open 20011109,2022: No such file or directory at ./AJnicify.pl line 69.
  50.  
  51. if (!($firstarg le $secondarg)) {
  52. die "1st arg must be < 2nd arg and of form 20010102,2346n";
  53. }
  54.  
  55. <>; #get command line out of the way of the stream
  56. <>; #first line (=unwanted header) gets lost intentionally
  57.  
  58. my $continue = 1;
  59. do # I use DO UNTIL instead of usual WHILE to get that extra match at the start
  60. {
  61. s/.{6},//; #works
  62. s/d+,d+(?=.*)/$&/;
  63. if ($& ge $firstarg) { $continue = 0; }
  64. } until (($continue == 0) || (!defined($_ = <>)));
  65.  
  66. $continue = 1;
  67. while ( (defined($_ = <>)) && ($continue == 1) )
  68. #: reads from stdin (& theoretically also command line args)
  69. {
  70. my $tmp = $_;
  71. $tmp =~ s/d+,d+(?=.*)/$&/;
  72. if ($& gt $secondarg) { $continue = 0; }
  73. s/([A-Z].....,)(.*)/2/; #remove first 6 capital letters
  74. s/([0-9]...)([0-9].)([0-9].),([0-9].)([0-9].)([0-9].)/1-2-3,4:5/;
  75. print $_;
  76. } # while
  77.  
  78. close STDERR;
  79.  
  80. } # two_args_sub
  81.  
  82. #XXXXXXXXXXXXXXXXXXXXXXX "MAIN" XXXXXXXXXXXXXXXXXXXXXXX
  83.  
  84. my $CL_len = $#ARGV + 1; #GLOBAL VARS; must do this BEFORE <>
  85.  
  86. $firstarg = $ARGV[0]; #print "firstarg=$firstargn";
  87. $secondarg = $ARGV[1]; #print "secondarg=$secondargn";
  88.  
  89. if ($CL_len == 2) { two_args_sub; }
  90. elsif ($CL_len == 0) { no_args_sub; }
  91. else {
  92. die "I quit - I needed no args or 2 args of form: 20010102,2346 (=2nd Jan 2001,23:46)n";
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement