Any unix scripters out there?
#1
Scooby Regular
Thread Starter
Join Date: Sep 2001
Location: A powerslide near you
Posts: 10,261
Likes: 0
Received 0 Likes
on
0 Posts
Any unix scripters out there?
Trying to figure out something that should be simple.
Have read a file and am looping through each line which is read into a $LINE variable OK.
Now, what I want to do is split that $LINE variable into it's constituent parts. The delimiter is a space.
Using ksh.
#There's gotta be a simple easy way to do this shirley.
Have read a file and am looping through each line which is read into a $LINE variable OK.
Now, what I want to do is split that $LINE variable into it's constituent parts. The delimiter is a space.
Using ksh.
#There's gotta be a simple easy way to do this shirley.
#4
Scooby Regular
Join Date: Oct 2000
Location: Surrey, UK
Posts: 8,384
Likes: 0
Received 0 Likes
on
0 Posts
Originally Posted by Dracoro
Trying to figure out something that should be simple.
Have read a file and am looping through each line which is read into a $LINE variable OK.
Now, what I want to do is split that $LINE variable into it's constituent parts. The delimiter is a space.
Using ksh.
#There's gotta be a simple easy way to do this shirley.
Have read a file and am looping through each line which is read into a $LINE variable OK.
Now, what I want to do is split that $LINE variable into it's constituent parts. The delimiter is a space.
Using ksh.
#There's gotta be a simple easy way to do this shirley.
(sh/ksh)
for WORD in $LINE
do
echo $WORD
done
Or do you want to look at the 5 field?
echo $LINE | cut -d' ' -f5
Or you can use 'nawk' (or awk)
echo $LINE | nawk -F' ' '{print $5}'
Plus loads of other ways... hopefully the above might help..
#6
Scooby Regular
Because that'll use loads of memory if it's a big file. For this stuff you need perl rather than shell/awk/sed because it was designed for it:
#!/usr/bin/perl -w
use strict;
open FILE, '/path/to/file' or die $!;
while (defined (my $line = <FILE>)) {
my @fields = split /\s+/, $line;
# @fields is an array of the line split on one or more whitespace
... do whatever with it
}
close FILE or die $!;
#!/usr/bin/perl -w
use strict;
open FILE, '/path/to/file' or die $!;
while (defined (my $line = <FILE>)) {
my @fields = split /\s+/, $line;
# @fields is an array of the line split on one or more whitespace
... do whatever with it
}
close FILE or die $!;
#7
Here you go this should do what you want:
while read LINE
do
echo $LINE | awk '{print $1}' >> /tmp/fname
echo $LINE | awk '{print $2}' >> /tmp/sname
done < /tmp/test
As tested on AIX ver 5.3 ML1 in KSH
The script above is an example of having first name and surname in a ascii text file.
$1 = First Name
$2 = Surname
The script makes the assumption that you want to show 2 fields from each line in the ascii text file.
while read LINE
do
echo $LINE | awk '{print $1}' >> /tmp/fname
echo $LINE | awk '{print $2}' >> /tmp/sname
done < /tmp/test
As tested on AIX ver 5.3 ML1 in KSH
The script above is an example of having first name and surname in a ascii text file.
$1 = First Name
$2 = Surname
The script makes the assumption that you want to show 2 fields from each line in the ascii text file.
Last edited by jpor; 08 February 2006 at 09:51 PM.
Trending Topics
#8
Scooby Regular
Join Date: Oct 2000
Location: Surrey, UK
Posts: 8,384
Likes: 0
Received 0 Likes
on
0 Posts
Originally Posted by stevencotton
Because that'll use loads of memory if it's a big file. For this stuff you need perl rather than shell/awk/sed because it was designed for it:
if he is new to Unix, perl might be a little scary too... (Sorry don't know your background D.)
And finally - Steve stop showing off
#10
Scooby Regular
Thread Starter
Join Date: Sep 2001
Location: A powerslide near you
Posts: 10,261
Likes: 0
Received 0 Likes
on
0 Posts
Cheers chaps, got it working. Small file so the awk command did the job. I use Perl for large files but in this instance it would have been overkill.
#11
Originally Posted by DrEvil
Agreed Perl is the best for this - is it me or does it actually not say what he wants to do with the 'fields' once he has them... Could make a difference to how complex or simple a script he needs...
if he is new to Unix, perl might be a little scary too... (Sorry don't know your background D.)
And finally - Steve stop showing off
if he is new to Unix, perl might be a little scary too... (Sorry don't know your background D.)
And finally - Steve stop showing off
#13
Originally Posted by stevencotton
It's spelled perl You don't need to use any non-core modules for simple things, I only used pragmata.
Although it depends on which platform he is coding on. I suppose for LINUX distros, Perl is included. But on some UNIX O/ses you have to make sure it has been installed with the BOS.
#14
Scooby Regular
Join Date: Oct 2000
Location: Surrey, UK
Posts: 8,384
Likes: 0
Received 0 Likes
on
0 Posts
Originally Posted by jpor
D'oh!
Although it depends on which platform he is coding on. I suppose for LINUX distros, Perl is included. But on some UNIX O/ses you have to make sure it has been installed with the BOS.
Although it depends on which platform he is coding on. I suppose for LINUX distros, Perl is included. But on some UNIX O/ses you have to make sure it has been installed with the BOS.
Thread
Thread Starter
Forum
Replies
Last Post