Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SCRIPT..help

Status
Not open for further replies.

YouEnjoyMyself

IS-IT--Management
Jul 27, 2004
111
US
Okay once again I need help...I am trying to take a tab delimited text file (Looking like the sample below) aand parse out some information. Want I want is just the Serial number column. I want that column to go out to a text file all as one line (see sample output) with each serial number seperated by a : . I appreciate any help. Thank you in advance.

Input:

Site Serial Number Medium Misplaced Storage

xxxx 001233 xxxxx xxxxx
xxxx 001234 xxxxx xxxxx
xxxx 001235 xxxxx xxxxx
xxxx 001236 xxxxx xxxxx
xxxx 001237 xxxxx xxxxx


Output:

001233:001234:001235:001236:001237

 
Code:
open(DFILE, "< filename.txt") || die "ERROR\n";
while(<DFILE>){
     chomp;
     $sn = (split(/\t/,$_))[1];
     $sn =~ s/^\s+//;
     $sn =~ s/\s+$//;
     if ($sn =~ /^[0-9]+$/) {
          push(@list, $sn);
     }
}
close(DFILE);

open(OUT, "> outfile.txt") || die "ERROR\n";
select(OUT);
$| = 1;
print OUT join(':', sort @list), "\n";
select(OUT);
$| = 1;
select(STDOUT);
$| = 1;
close(OUT);

exit(0);



Michael Libeson
 
michael,
wouldn't split /\s+/,$_ negate the need for the trim functions
--Paul

cigless ...
 
The file is tab delimited, but there could be whitespaces that are not tabs that should be removed, or should they?


Michael Libeson
 
I should clarify a little.

There may be white spaces within the tab delimited fields so you may not use \s to delimit the fields. The user specified the files as tab delimited so I explicitly used \t and not \s for this reason. It will be up to the user depending on the data in the file whether to use \s or \t.


Michael Libeson
 
No worries,

I had a code snippet ready to post, and the only real difference was the \t, trim vs \s.

TIMTOWTDI as they say,
--Paul
Code:
open FH, "<myfile.txt";
open OUT, ">serials.txt";
$first=":";
while (<FH>) {
   $serial=(split (/\s+/,$_)[1];
   print OUT "$first$serial";
   $first=":";
)
close FH;
close OUT;

cigless ...
 
Code:
BEGIN{FS="\t";ORS=""}NR>1{print":"}{print$2}
[tt]
xxxx 001233 xxxxx xxxxx
xxxx 001234 xxxxx xxxxx
xxxx 001235 xxxxx xxxxx
xxxx 001236 xxxxx xxxxx
xxxx 001237 xxxxx xxxxx
[/tt]
becomes
[tt]
001233:001234:001235:001236:001237
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top