The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to store values into variable in perl chittiprasad15 Linux 3 05-08-2008 01:21 AM
Need help to write a Perl script user_prady Shell Programming and Scripting 10 03-26-2008 02:38 AM
how to get last value in an array in perl meghana Shell Programming and Scripting 7 02-04-2008 05:12 PM
formating array file output using perl seismic_willy Shell Programming and Scripting 4 03-22-2007 02:23 AM
need help to write perl code getdpg Shell Programming and Scripting 0 09-20-2006 10:24 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-03-2009
durden_tyler's Avatar
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2009
Posts: 547
Quote:
Originally Posted by schultz2146 View Post
...
So how can we trap colums with alphanum without catching everything?
...
With the [:alnum:] POSIX character class.

tyler_durden
  #2 (permalink)  
Old 06-07-2009
schultz2146 schultz2146 is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 8
So if below works on a single column datafile of
Apple
B34
Cat
112
245
356

Outputs to screen:
112 Apple
245 B34
356 Cat

open(FILE, "<", "dataFile");
while(<FILE>) {
chomp;
if ($_ =~ /^[[:digit:]]+$/) {
push @intArray, $_;
} elsif (($_ =~ /^[[:alpha:]]+$/)||($_ =~ /^[[:alnum:]]+$/)) {
push @stringArray, $_;
}
}
close(FILE);
$lengthArray=@intArray;
for($i=0; $i<$lengthArray; $i++) {
print"\n$intArray[$i] $stringArray[$i] ";
}



How can I take a datafile that has a special character and have mult arrays. First array catches everything before the special character “$” and the second array catches everything after the special character?

Abc is good for me $4.55
Def might not be enough $5.66
Ghi price to play $6.77

/r
Rick
  #3 (permalink)  
Old 06-07-2009
durden_tyler's Avatar
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2009
Posts: 547
Quote:
Originally Posted by schultz2146 View Post
...
How can I take a datafile that has a special character and have mult arrays. First array catches everything before the special character “$” and the second array catches everything after the special character?

Abc is good for me $4.55
Def might not be enough $5.66
Ghi price to play $6.77

...

Code:
$
$ cat data.txt
Abc is good for me $4.55
Def might not be enough $5.66
Ghi price to play $6.77
$
$ perl -ne 'chomp; split /\$/; push @a1,$_[0]; push @a2,$_[1]; END{foreach $i(@a1){print $i,"\n"} foreach $i(@a2){print $i,"\n"}}' data.txt
Abc is good for me
Def might not be enough
Ghi price to play
4.55
5.66
6.77
$
$

tyler_durden
  #4 (permalink)  
Old 06-07-2009
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
In script form:


Code:

my @before;
my @after;
open(FILE, "<", "dataFile");
while(<FILE>) {
   chomp;
   my ($before, $after) = split(/\$/);
   push @before, $before;
   push @after, $after;
}

for my $i (0 .. $#before) {
   print "$before[$i] $after[$i]\n";
}

Assumes all lines have the '$' character in them, if not, adjust accordingly.
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 01:25 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0