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. |
|
From memory, you would want to use awk or sed (havent done scripting for around 8 years though!
|
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. (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.. |
why not split each line into an array?
|
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 $!; |
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. |
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 ;) |
Originally Posted by DrEvil
And finally - Steve stop showing off ;)
|
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.
|
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 ;) |
It's spelled perl :) You don't need to use any non-core modules for simple things, I only used pragmata.
|
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. |
Originally Posted by jpor
D'oh! :lol1:
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. |
Originally Posted by DrEvil
Yeah, AIX is different to Solaris / Linux in that way.
|
All times are GMT +1. The time now is 10:04 AM. |
© 2024 MH Sub I, LLC dba Internet Brands