Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to read items from an array 2

Status
Not open for further replies.

Jimbo2112

IS-IT--Management
Mar 18, 2002
109
GB
Hi All,

I am writing a script where I need to read items of delimited data from a file which I have created earlier in the script ( a bit like Blue Peter, UK folks only!). With each data item that I read, which is delimited by a colon, I need to make a separate file whose name is taken from the item of data which has just been read. See below:

data file:

63748: 75836: 98374: 36485:

Read data from array into loop (e.g. 63478: )
take delimited item and create file named after the item (e.g. 63478)
read next item
loop until all data has been used


Could you brainy script bods give me the command line sequence to be able to complete this task?

Cheers!

Jimbo
 
Jimbo - Try this - the files will be empty - is that what you want? (In this example your datafile is called "items".)

for inx in `cat items | tr : "\n"`
do
> $inx
done
 
Thanks for your swift response!

I am going to have a little dabble with this no and see if I can make it fit into my script and will let you know how I get on.

Cheers!

Jimbo
 
Or, assuming you don't have any spaces in your delimited list ...

IFS=":"
for i in $(cat list)
do
> $i
done

Greg.
 
Hi:

If you go to the trouble of changing the field separator, how about saving yourself one shell invocation:

IFS=":"
while read i a b c
do
> $i
done < list

With a small file it won't make any difference, but with a large file it might.

The above assumes each line has 4 fields, : delimited, and you only want to look at the first field.

Regards,


Ed

 
well... How 'bout yet another variation on the same theme - let's keep the changes to IFS local to the 'loop':

while IFS=&quot;:&quot; read i a b c
do
> $i
done < list vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Thanx for the responses ........ but I need the equivalent code in c-shell please!

Sorry, but the software that I will be using the script in conjunction with runs based on c-shell.

Cheers

Jimbo
 
Vlad:

You get a star. I didn't know you could do this with IFS:

while IFS=&quot;:&quot; read i a b c


Ed
 
comp.unix.shell and comp.lang.awk RULE ;)

vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
OK,

I think the easiest way to fix this is with a data file that has no spaces and just : for delimiters. I still need the fix in c-shell though!

I now it is deemed as old fashioned, but my hands are tied! :-(

Thanx all!

Jimbo
 
In C-shell we have to go back to one of the first solutions:

foreach file (`sed -e 's/ *: */:/g' list | tr ':' '\n'`)
> $file
end

The sed is used to cope with spaces around ':' field separators.

To vlad (and just to be captious) :
the change to IFS is not local to the 'loop', it is local to the 'read'. This is the standard structure that allows to declare environment variable value only during the execution of a command:

VAR1=value1 [VAR2=value2 ... ] command arguments

For example you can do:

date
TZ=GMT date
date

The second execution of date will use the GMT time zone, but not the first nor the third. The environment value of TZ is only changed for the following simple command on the same line.

Thus, in

while IFS=':' read i a b c; do
read ii aa bb cc
done

IFS is changed only for the first read. The read inside the loop use the previous value of IFS.
 
perfect explanation - mucho gracias!!

vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Thanx all,

Yet again you have served me well. I will employ your solution next week!

have a nice weekend!

Cheers,

Jimbo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top