Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for cut.pl

(view source code of cut.pl as plain text)

  1. #! perl
  2.  
  3. $syntax = "\nCut.pl,  Version 1.11\nPort of Unix' CUT command\n\n";
  4. $syntax = $syntax."Usage:\n\nany_cmd | PERL CUT.PL [-DEBUG] ";
  5. $syntax = $syntax."{-C:n|-F:n [-D:'any_string' [-I]]} [-L:n] [-S]\n\n";
  6. $syntax = $syntax."Argument:           Function:";
  7. $syntax = $syntax."                                 Dependency:\n";
  8. $syntax = $syntax."=========           =========";
  9. $syntax = $syntax."                                 ===========\n";
  10. $syntax = $syntax."any_cmd             command that's output is to be parsed";
  11. $syntax = $syntax."\n-DEBUG              display intermediate results\n";
  12. $syntax = $syntax."                    (preferably the first argument)\n";
  13. $syntax = $syntax."-C:<column_number>  parse by Columns or Characters\n";
  14. $syntax = $syntax."-D:'<delimiter>'    Delimiter character or string";
  15. $syntax = $syntax."             -F\n";
  16. $syntax = $syntax."                    (should be escaped if necessary)\n";
  17. $syntax = $syntax."-F:<field_number>   parse by Fields or words\n";
  18. $syntax = $syntax."-I                  case Insensitive delimiter";
  19. $syntax = $syntax."                -D\n";
  20. $syntax = $syntax."-L:<length>         number of characters or words ";
  21. $syntax = $syntax."to display  -C\n";
  22. $syntax = $syntax."-S                  Skip blank lines in result\n\n";
  23. $syntax = $syntax."Written by Rob van der Woude\n";
  24. $syntax = $syntax."http://www.robvanderwoude.com\n";
  25.  
  26. # Assign default values
  27. $case  = 1;
  28. $debug = 0;
  29. $delim = "\\s";
  30. $help  = 0;
  31. $len   = 0;
  32. $skip  = 0;
  33.  
  34. # Command line parsing is SO much easier using regular expressions
  35. foreach $_ ( @ARGV ) {
  36. 	if ( $debug == 1 ) {
  37. 		print "$_\n";
  38. 	}
  39. 	SWITCH: {
  40. 		if ( $_ =~ m/^-C:(\d+)$/i ) {
  41. 			$type = "char";
  42. 			$char = $1;
  43. 			last SWITCH;
  44. 		}
  45. 		if ( $_ =~ m/^-D:\'?([^.']+)\'?$/i ) {
  46. 			$delim = $1;
  47. 			last SWITCH;
  48. 		}
  49. 		if ( $_ =~ m/^-DEBUG$/i ) {
  50. 			$debug = 1;
  51. 			print "\nCommand line arguments:\n$_\n";
  52. 			last SWITCH;
  53. 		}
  54. 		if ( $_ =~ m/^-F:(\d+)$/i ) {
  55. 			$type = "word";
  56. 			$word = $1;
  57. 			last SWITCH;
  58. 		}
  59. 		if ( $_ =~ m/^-I$/i ) {
  60. 			$case = 0;
  61. 			last SWITCH;
  62. 		}
  63. 		if ( $_ =~ m/^-L:(\d+)$/i ) {
  64. 			$len = $1;
  65. 			last SWITCH;
  66. 		}
  67. 		if ( $_ =~ m/^-S$/i ) {
  68. 			$skip = 1;
  69. 			last SWITCH;
  70. 		}
  71. 		$help = 1;
  72. 	}
  73. }
  74.  
  75. if ( !@ARGV[0] or ( $help == 1 ) ) {
  76. 	print $syntax;
  77. 	exit(1);
  78. }
  79.  
  80. # Debug info
  81. if ( $debug == 1 ) {
  82. 	print "\nInterpretation of command line arguments:\n";
  83. 	print "\$case=$case\n\$char=$char\n\$delim=$delim\n";
  84. 	print "\$len=$len\n\$skip=$skip\n\$type=$type\n\$word=$word\n";
  85. }
  86.  
  87. # Create the regular expressions to parse ("cut") the string
  88. $re = "^";
  89. if ( $type eq "word" ) {
  90. 	if ( $case == 0 ) {
  91. 		$re = $re."^(?i)";
  92. 	}
  93. 	$re = $re."(?:$delim)*";
  94. 	if ( $word > 1 ) {
  95. 		for ( $i = 1; $i < $word; $i++ ) {
  96. 			$re = $re."[^$delim]+(?:$delim)+";
  97. 		}
  98. 	}
  99. 	$re  = $re."(";
  100. 	$re0 = $re.".*)\$";
  101. 	if ( $len > 0 ) {
  102. 		$re = $re."[^$delim]+";
  103. 		for ( $i = 1; $i < $len; $i++ ) {
  104. 			$re = $re."(?:$delim)+[^$delim]+";
  105. 		}
  106. 		$re = $re.")(?:$delim)";
  107. 	} else {
  108. 		$re = $re0;
  109. 	}
  110. 	if ( $case == 0 ) {
  111. 		$re = $re."(?-i)";
  112. 	}
  113. } else {
  114. # if ( $type eq "char" ) {
  115. 	if ( $char > 0 ) {
  116. 		$re  = $re.".{".( $char - 1 )."}";
  117. 		$re0 = $re."(.*)\$";
  118. 	}
  119. 	if ( $len > 0 ) {
  120. 		$re = $re."(.{0,$len})";
  121. 	} else {
  122. 		$re = $re0;
  123. 	}
  124. }
  125.  
  126. # Debug info
  127. if ( $debug == 1 ) {
  128. 	print "\nRegular Expression:\n$re\n";
  129. 	print "\nStandard input \& \"cut\" result:\n";
  130. }
  131.  
  132. # Parse STDIN and display results
  133. while ( <STDIN> ) {
  134. 	chomp $_;
  135. 	if ( $debug == 1 ) {
  136. 		print "STDIN: $_\n";
  137. 	}
  138. 	$m = $_;
  139. 	if ( $_ =~ $re ) {
  140. 		print "$1\n";
  141. 	} elsif ( $_ =~ $re0 ) {
  142. 		print "$1\n";
  143. 	} elsif ( $skip == 0 ) {
  144. 		print "\n";
  145. 	}
  146. }
  147.  

page last uploaded: 2017-08-21, 14:26