Scannig files, C time routine help.

Scannig files, C time routine help.

Post by bart.. » Sat, 24 Feb 1996 04:00:00



Hello,

        Here's a couple that should be easy.

        First, I have a file with 1000 lines. Each line has the following
format. a line number, followed by a colon (:), followed by a word, followed by
another colon, followed by the line number again.

        Now, I can change that format if necessary. That's not *real*
important.  But anyway, using a shell script, I want to be able to scan that
file and get that word between the two colons, then assign it to a variable for
later use. I'll continue to look through shell scripts for examples that I
might be able to use, but in the meantime, I thought I'd throw it out here for
the world to see.

        Second,  I have written a C program to create the above mentioned
file. The "word" I'm creating is a 8 character random password.  Anyway, I need
to "seed" the password generator.  What routines are available in C to glean
the numbers from the system clock. I can then use that as a seed.

Thanks in advance,

Lyndon F. Bartels

 
 
 

Scannig files, C time routine help.

Post by Tom Sande » Sat, 24 Feb 1996 04:00:00


|> Hello,
|>
|>   Here's a couple that should be easy.
|>
|>   First, I have a file with 1000 lines. Each line has the following
|> format. a line number, followed by a colon (:), followed by a word, followed by
|> another colon, followed by the line number again.
|>
|>   Now, I can change that format if necessary. That's not *real*
|> important.  But anyway, using a shell script, I want to be able to scan that
|> file and get that word between the two colons, then assign it to a variable for
|> later use. I'll continue to look through shell scripts for examples that I
|> might be able to use, but in the meantime, I thought I'd throw it out here for
|> the world to see.

in ksh:  set -A word_list `cut -d: -f2 $file`
This puts the 2nd column of the file (delimited by :) into an array called
word_list.  
To access an element N:  ${word_list[N]}
To access all elements:  ${word_list[*]}

or in a loop:
for word in `cut -d: -f2 $file`
do
    echo $word
done

|>
|>
|>
|>
|>
|>   Second,  I have written a C program to create the above mentioned
|> file. The "word" I'm creating is a 8 character random password.  Anyway, I need
|> to "seed" the password generator.  What routines are available in C to glean
|> the numbers from the system clock. I can then use that as a seed.
|>
|>
|>
|> Thanks in advance,
|>
|> Lyndon F. Bartels


 
 
 

Scannig files, C time routine help.

Post by David and Rachel Weintra » Sun, 25 Feb 1996 04:00:00




> Hello,

>         Here's a couple that should be easy.

>         First, I have a file with 1000 lines. Each line has the following
> format. a line number, followed by a colon (:), followed by a word,
followed by
> another colon, followed by the line number again.

>         Now, I can change that format if necessary. That's not *real*
> important.  But anyway, using a shell script, I want to be able to scan that
> file and get that word between the two colons, then assign it to a
variable for
> later use. I'll continue to look through shell scripts for examples that I
> might be able to use, but in the meantime, I thought I'd throw it out here for
> the world to see.

If you want to do the whole file at once:

awk -F: '{print $2}' fileName > newFile

This will just print the words you want to a new file.

To do this in a Korn Shell Script:

    #!/bin/ksh
    OLDIFS="$IFS"
    IFS=":"
    fileName=tempFile
    cat $fileName | while read line word rest_of_line
    do
        array[$line]="$word"
    done
    IFS="$OLDIFS"

You can access the word with "${array[${line}]}"

To do this in a Bourne Shell:

    #!/bin/ksh
    OLDIFS="$IFS"
    IFS=":"
    fileName=tempFile
    cat $fileName | while read line word rest_of_line
    do
        array,$line="$word"
    done
    IFS="$OLDIFS"

You can access the word with array.$line"

--
David Weintraub                    _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
System Configuration Management   _/                                      _/
Dow Jones/Telerate - NSG         _/    I AM THE GREAT AND POWERFUL OZ*   _/


                           *Pay no attention to the man behind the curtains

 
 
 

Scannig files, C time routine help.

Post by Heiner Stev » Fri, 01 Mar 1996 04:00:00




[...]
 > >         First, I have a file with 1000 lines. Each line has the following
 > > format. a line number, followed by a colon (:), followed by a word,
 > followed by
 > > another colon, followed by the line number again.
[...]

 > To do this in a Korn Shell Script:
 >     #!/bin/ksh
 >     OLDIFS="$IFS"
 >     IFS=":"
 >     fileName=tempFile
 >     cat $fileName | while read line word rest_of_line
 >     do
 >         array[$line]="$word"
 >     done
 >     IFS="$OLDIFS"

There is no need to use cat here. You can always replace

        cat FILE | COMMAND

with

        COMMAND < FILE

, thus saving one process and speeding up your shell script.
Rewrite the while loop as follows:

        while read line word rest_of_line
        do
            ...
        done < "$filename"

Heiner
--
      -------------------------------------------------------------

    / BinTec Computersysteme GmbH / mine, not BinTec's --       /
   / Willstaetter Str. 30 -------   ...even if they should ;-) /

 -------------------------------------------------------------