ScoobyNet.com - Subaru Enthusiast Forum

ScoobyNet.com - Subaru Enthusiast Forum (https://www.scoobynet.com/)
-   Computer & Technology Related (https://www.scoobynet.com/computer-and-technology-related-34/)
-   -   Any unix scripters out there? (https://www.scoobynet.com/computer-and-technology-related-34/490835-any-unix-scripters-out-there.html)

Dracoro 08 February 2006 04:43 PM

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.

dynamix 08 February 2006 04:47 PM

http://uk.php.net/manual/en/function.split.php

will that do it?

TonyFlow 08 February 2006 05:21 PM

From memory, you would want to use awk or sed (havent done scripting for around 8 years though!

DrEvil 08 February 2006 06:13 PM


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.

And then what, do you want to process each 'word'?

(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..

chump 08 February 2006 06:13 PM

why not split each line into an array?

stevencotton 08 February 2006 07:09 PM

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 $!;

jpor 08 February 2006 09:46 PM

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.

DrEvil 09 February 2006 12:02 AM


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:

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 ;)

stevencotton 09 February 2006 10:31 AM


Originally Posted by DrEvil
And finally - Steve stop showing off ;)

s/.+e\/?.(e)n (.+t(...))/\/ \10\10Just another Perl hacker/; :D

Dracoro 09 February 2006 10:49 AM

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.

jpor 09 February 2006 05:54 PM


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 also depends if the Pearl module is installed as well. Or running Pearl scripts would not be an option.

stevencotton 09 February 2006 08:07 PM

It's spelled perl :) You don't need to use any non-core modules for simple things, I only used pragmata.

jpor 10 February 2006 12:01 AM


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.

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.

DrEvil 10 February 2006 11:04 AM


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.

Yeah, AIX is different to Solaris / Linux in that way.

jpor 10 February 2006 06:15 PM


Originally Posted by DrEvil
Yeah, AIX is different to Solaris / Linux in that way.

Indeed it is :thumb:


All times are GMT +1. The time now is 10:04 AM.


© 2024 MH Sub I, LLC dba Internet Brands