ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Im trying to take a pipe delimited file and remove the white spaces except for particular fields specified. I would like this ARG to accept multiple files but I havnet got that far yet. For some reason its not working.. if someone could please help, it would be most appreciated.
Code:
$SourceFile = $ARGV[0];
$FieldNum = $ARGV[1];
open(INFILE, $SourceFile) or die "Can't open source file: $SourceFile \n";
open(OutGood, "> TEST.good.txt") or die "Can't open output file \n";
while(<INFILE>)
{
@fields = split /\|/, $_;
if ($FieldNum != $fields){
s/\s+//g;
}
print OutGood join '|', @fields;
}
without any further information, i suspect you want to check $FieldNum against one of the elements in @fields??? to access the elements, you need to do something like $fields[0] or $fields[1] etc...
First Name|Date of Birth| City | State | phone
Joe Smith |01 01 1990 |New York|NY|212 555 1212
Im curious how I can remove all white spaces except for the fields specified from the command line ( ARGV[1] ).
so if thats my input file, and I entered
$script.pl 1,3
the output would be
Joe Smith |01011990|New York|NY|2125551212
this is just an example, the actual input file has many many more fields which is why I want to specify which fileds NOT to remove white spaces rather than which fields to remove.
I took your script and changed it a bit. I added some debugging prints so that you can enable them to see intermediate data transformations. To do that, just interchange the debug statements in the code below. I usually write to STDOUT so that I can redirect the output as I chose.
Keep in mind that this is a skeleton of one way to handle the data, and you might need to add to it to suit your situation.
Run the code, figure out what it is doing and if you have questions that are not answered by looking in a perl book, then feel free to ask ... cheers, makyo (73)
#!/usr/bin/perl
Code:
#!/usr/bin/perl
# @(#) p1 Demonstrate perl features.
use warnings;
use strict;
my $debug;
$debug = 1;
$debug = 0;
my $t1 = shift || die "Cannot read fields.\n";
print "Fields read $t1:\n" if $debug;
my @squeezed = split /,/, $t1;
my @fields;
while ( <> ) {
chomp;
print "input :$_:\n" if $debug;
@fields = split /[|]/;
print "split ",$#fields+1," from line $.\n" if $debug;
foreach my $i ( @squeezed ) {
print "squeezing field $i :$fields[$i]: in line $.\n" if $debug;
$fields[$i] =~ s/\s+//g;
}
print join("|",@fields),"\n";
}
When run on your data line:
Code:
% ./p1 1,4 data1
Joe Smith |01011990|New York|NY|2125551212
Oh wow that great, and its teaching me a lot more about perl in general. this is pretty far over my head but I dot have Learning Perl and The Perl cookbook by O'Reilly to help me sort out some of the commands Im not familiar with.
Just one question, since there are more fields to remove white than not, how hard is it to reverse? (ie 1, 4 will NOT remove white from those fields?)
Once you have written a number of programs in a language you will probably be able to think in that language, and, for example, your fingers will seem to know to type in a ";" at the end of a perl statement, etc. I believe this is similar to knowing a natural language well enough so that you dream in it. What I call "makyo's limit" for this number is around 100.
To modify the program from changing a field to not changing, I recommend that you think about a way that would consider every field, and if the field is not to be squeezed, then skip over that. That means you need to see how many fields there are and have a loop that looks at every field. Chapters 3 and 10 in Learning Perl, 3rd Edition discuss control structures.
Get the job done, but don't be afraid to experiment by writing little scripts to make sure you understand some points of the language. Remember that this is one way to accomplish the specific task you are addressing, and that there are others ... cheers, makyo
import sys
try:
input_options = [ int(i) - 1 for i in sys.argv[1].split(",") ]
except : pass
all = open("input.txt").readlines()
for items in all:
try:
getall = items.split("|")
except: pass
for num in input_options:
try:
getall[num] = getall[num].replace(" ","")
except: pass
print '|'.join(getall)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.