Proud member of the Firebagger Lefty blogosphere!

Sunday, April 08, 2007

concatXlines.pl - the concatenator!

Somebody needed a program to put 4 contiguous lines of a file on the same line starting from the beginning of the file and going to the end.

Well, here it is with a simple improvement - you can set the number of lines to concatenate. One notice - this works on Linux - Unix files. A Perl program to convert Windows files with carriage returns (\r) and end of line (\n) characters to Unix/Linux files with only end of line characters can be found here.

You can find this program here.

..........................
#!/usr/bin/perl -w
#
# Author: Michael W Folsom
# April 8, 2007
#
# USAGE: ./concatXlines.pl num_lines_to_concat input_file_name
#
# PURPOSE: To cocatenate date from X contiguous lines to a single
# line. For example if the data is organized like so:
# 1
# 2
# 3
# 4
#
# And the program is told to convert 4 lines to 1 you would get:
# 1 2 3 4
#
#

# stash the number of arguments
# $#ARGV + 1 is ARGC in C, C++ land
$argNumber = $#ARGV + 1;

sub usageStatement {
print "\nUsage: ./concatXlines.pl num_lines_to_concat input_file_name\n\n";
}

if($argNumber != 2) {
&usageStatement();
exit(1);
}

# use the open command to open the input file for reading
open(FILE, $ARGV[1]);

$numLines = $ARGV[0];
$newStr ="";
$i = 0;

while () {
# chop off newline character
chomp($_);

# chugger through file X lines at a time and build up new
# output line
if($i < $numLines) {
$newStr = $newStr . $_;
$i++;
if($i < $numLines) {
#print "\t";
$newStr = $newStr . "\t";
}
# got data from X lines, print and add a new line
else {
print "$newStr \n";
$i= 0;
$newStr = "";
}
}
}

# if number of lines not equal to 4 then print out remainder
if($newStr) {
print "$newStr\n";
}

close(FILE);

exit();
..........................

1 comment:

Unknown said...

Found your blog through your comments on TPM this morning. You were correct in your analysis of the Journal's coverage of Saint Pete and Iglesias. Have you noticed that Whitney Cheshire at Wednesday Morning QB has stopped posting on her blog? Mario Burgos has just filed a lame FOIA request for information on David Iglesias' official travel during his tenure as USA. They have spent the last two months smearing this guy. Now we find out just how crooked they really are. I doubt I have much in common politically with Iglesias but he does appear to be an honorable fellow. Good blog!