I have a file of format:

BRAND VERSION MODEL OPTIONS BOUGHT
------ ------- ------- ------- ----------
toyota lxi 2007 4 years ago
nissan mxi 2008 3 years ago

Actually the formatting goes awry when I save-

So its Brand(toyota nissan under it)
VERSION(lxi mxi under it)
MODEL(2007 2008)
OPTIONS -this column is empty
BOUGHT( 4 years ago n 3 years ago)

when I try to pick a row 3 (MODEL) out of it-

cat car.out |perl -ane 'print $F[2]."\n";'

MODEL
-------
2007
2008

This is as expected

Now I try to pick a row 4 (OPTIONS)

cat car.out |perl -ane 'print $F[3]."\n";'

OPTIONS
-------
4
3


The output is incorrect since this column is empty.

Any thoughts on what a workaround could be. I dont want to put in any defualt values like NUL in the OPTIONS Column & also I can;t assume this column is always empty, at times this will have valid entries.

SO need something that would output
OPTIONS
-------


Thanks in advance
David

Edited 5 Years Ago by blacknred: n/a

We need to know what delimiter character you use to separate the columns in your file. When you post the data without wrapping it in [code]

[/code] tags all duplicate spaces or tabs become single spaces so we can't see some of the delimiters. If you put only one single space between the MODEL value and the BOUGHT value then the BOUGHT value appears in the OPTIONS column. Also any value that contains spaces, such as '4 years ago' will split into three separate columns if you use a single space as the delimiting character. Instead, use a delimiter character that will never appear in the values, such as tilda ~. Then your file would look like the following (without the line numbers, of course):

BRAND~VERSION~MODEL~OPTIONS~BOUGHT
toyota~lxi~2007~~4 years ago
nissan~mxi~2008~~3 years ago

Note that the two consecutive delimiters (~~) indicate an empty column.

If you already have proper delimiters that we can't see, post your data wrapped in [code]

[/code] tags or else attach them to your reply as a '.txt' file attachment.

Emm.. the number of white spaces correspong to number of dashes '-' s which is the width of each column.

BRAND VERSION MODELS OPTIONS BOUGHT
----- ------- ------ ------- ----------
toyot lxi     2007           4 years ago
niss  mxi     2008           3 years ago

Here's what I tried:

open THEFILE, "car.out";
#@wholeThing = <THEFILE>;
$text = <THEFILE>;
#print "the text is $text";
close THEFILE;
my $he;
chomp($text);
my $i=1;
@words = split(/ /, $text);
foreach $wordIn (@words){
        my $headerstr = $he.'$'.$wordIn.',';
        $he = $headerstr;
        #print "he in loop is ", $he,"\n";
        #print "heiader in loop is ", $he,"\n";
                                }
chop($he);
print "final value of he outside loop is ", $he,"\n";


open THEFILE, "car.out";
@wholeThing = <THEFILE>;
close THEFILE;

my $grab;
foreach $line (@wholeThing){
        if ($line =~ m/--/){
        
        chomp($line);
        @words = split(/ /, $line);

        foreach $wordIn (@words){

	        chomp($wordIn);
 
        	my $fi = length($wordIn);
#        	print "column header is $wordIn with lengt $fi\n";

 #       	print "grab is $grab\n";
        	my $newgrab = $grab.'A'.$fi.'x';
        	$grab = $newgrab;
		}
         }
}		
chop ($grab);
chop($grab);
print "final regex is ", $grab,"\n";

while (<>) {

[B]#my($he) = unpack("$grab", $_);[/B]
my($BRAND,$MODEL,$VERSION,$OPTIONS,$BOUGHT) = unpack("$grab", $_);
print "$OPTIONS\n";
}

so (to print out the OPTIONS column)
(//commenting out $he for the momemnt)

cat car.out | perl script.pl

final value of he outside loop is $BRAND,$VERSION,$MODELS,$OPTIONS,$BOUGHT
final regex is A5xA7xA6xA7xA1
OPTIONS
-------

works :)

However my intention is not to hard code
my($BRAND,$MODEL,$VERSION,$OPTIONS,$BOUGHT)

I have been successful in extracting that value from file & its stored in $he as seen in above ouput

final value of he outside loop is $BRAND,$VERSION,$MODELS,$OPTIONS,$BOUGHT

But when I uncomment it & run:

cat car.out |perl script.pl
final value of he outside loop is $BRAND,$VERSION,$MODELS,$OPTIONS,$BOUGHT
final regex is A5xA7xA6xA7xA1

ie OPTION column is not printed, so seems the values of $he are not getting substitued for $BRAND,$VERSION,$MODELS,$OPTIONS,$BOUGHT
:(

Edited 5 Years Ago by blacknred: n/a

This article has been dead for over six months. Start a new discussion instead.