Notices
Computer & Technology Related Post here for help and discussion of computing and related technology. Internet, TVs, phones, consoles, computers, tablets and any other gadgets.

Any unix scripters out there?

Thread Tools
 
Search this Thread
 
Old 08 February 2006, 04:43 PM
  #1  
Dracoro
Scooby Regular
Thread Starter
 
Dracoro's Avatar
 
Join Date: Sep 2001
Location: A powerslide near you
Posts: 10,261
Likes: 0
Received 0 Likes on 0 Posts
Default 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.
Old 08 February 2006, 04:47 PM
  #2  
dynamix
Former Sponsor
iTrader: (3)
 
dynamix's Avatar
 
Join Date: Feb 2006
Location: near you
Posts: 9,708
Likes: 0
Received 3 Likes on 3 Posts
Default

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

will that do it?
Old 08 February 2006, 05:21 PM
  #3  
TonyFlow
Scooby Regular
 
TonyFlow's Avatar
 
Join Date: Jan 1999
Posts: 1,293
Likes: 0
Received 0 Likes on 0 Posts
Default

From memory, you would want to use awk or sed (havent done scripting for around 8 years though!
Old 08 February 2006, 06:13 PM
  #4  
DrEvil
Scooby Regular
 
DrEvil's Avatar
 
Join Date: Oct 2000
Location: Surrey, UK
Posts: 8,384
Likes: 0
Received 0 Likes on 0 Posts
Thumbs up

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..
Old 08 February 2006, 06:13 PM
  #5  
chump
Scooby Regular
 
chump's Avatar
 
Join Date: Feb 2004
Posts: 266
Likes: 0
Received 0 Likes on 0 Posts
Default

why not split each line into an array?
Old 08 February 2006, 07:09 PM
  #6  
stevencotton
Scooby Regular
 
stevencotton's Avatar
 
Join Date: Jan 2001
Location: behind twin turbos
Posts: 2,710
Likes: 0
Received 1 Like on 1 Post
Default

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 $!;
Old 08 February 2006, 09:46 PM
  #7  
jpor
Scooby Regular
iTrader: (1)
 
jpor's Avatar
 
Join Date: Sep 2003
Posts: 3,109
Likes: 0
Received 0 Likes on 0 Posts
Default

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.

Last edited by jpor; 08 February 2006 at 09:51 PM.
Old 09 February 2006, 12:02 AM
  #8  
DrEvil
Scooby Regular
 
DrEvil's Avatar
 
Join Date: Oct 2000
Location: Surrey, UK
Posts: 8,384
Likes: 0
Received 0 Likes on 0 Posts
Wink

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
Old 09 February 2006, 10:31 AM
  #9  
stevencotton
Scooby Regular
 
stevencotton's Avatar
 
Join Date: Jan 2001
Location: behind twin turbos
Posts: 2,710
Likes: 0
Received 1 Like on 1 Post
Default

Originally Posted by DrEvil
And finally - Steve stop showing off
s/.+e\/?.(e)n (.+t(...))/\/ \10\10Just another Perl hacker/;
Old 09 February 2006, 10:49 AM
  #10  
Dracoro
Scooby Regular
Thread Starter
 
Dracoro's Avatar
 
Join Date: Sep 2001
Location: A powerslide near you
Posts: 10,261
Likes: 0
Received 0 Likes on 0 Posts
Default

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.
Old 09 February 2006, 05:54 PM
  #11  
jpor
Scooby Regular
iTrader: (1)
 
jpor's Avatar
 
Join Date: Sep 2003
Posts: 3,109
Likes: 0
Received 0 Likes on 0 Posts
Default

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.
Old 09 February 2006, 08:07 PM
  #12  
stevencotton
Scooby Regular
 
stevencotton's Avatar
 
Join Date: Jan 2001
Location: behind twin turbos
Posts: 2,710
Likes: 0
Received 1 Like on 1 Post
Default

It's spelled perl You don't need to use any non-core modules for simple things, I only used pragmata.
Old 10 February 2006, 12:01 AM
  #13  
jpor
Scooby Regular
iTrader: (1)
 
jpor's Avatar
 
Join Date: Sep 2003
Posts: 3,109
Likes: 0
Received 0 Likes on 0 Posts
Default

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!

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.
Old 10 February 2006, 11:04 AM
  #14  
DrEvil
Scooby Regular
 
DrEvil's Avatar
 
Join Date: Oct 2000
Location: Surrey, UK
Posts: 8,384
Likes: 0
Received 0 Likes on 0 Posts
Default

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.
Yeah, AIX is different to Solaris / Linux in that way.
Old 10 February 2006, 06:15 PM
  #15  
jpor
Scooby Regular
iTrader: (1)
 
jpor's Avatar
 
Join Date: Sep 2003
Posts: 3,109
Likes: 0
Received 0 Likes on 0 Posts
Default

Originally Posted by DrEvil
Yeah, AIX is different to Solaris / Linux in that way.
Indeed it is
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
dogmaul
Computer & Technology Related
17
03 May 2003 12:46 PM
Dracoro
Computer & Technology Related
7
07 April 2003 10:58 AM
Dracoro
Computer & Technology Related
9
25 February 2003 03:08 PM
SD
Computer & Technology Related
5
24 April 2002 10:29 AM



Quick Reply: Any unix scripters out there?



All times are GMT +1. The time now is 01:29 AM.