Dynamic file names

D

Darren Stribling

Hi Guys and Girls,

I'm trying to read from a file

The file contains call numbers, a variable amount.

The call numbers come after an identifier NOTF

I then want to put all the data after NOTF into a file, but call the file
the name of the unique call number.

so:

while (file not empty)
read (input file) // line by line
if (input file) = NOFT
create file (name of preceding call number)
put (data after NOTF)
until (next NOTF)
}
}
}

Sorry for the VERY bad psuedo code.

Regards,

Darren
 
A

Alan Connor

Hi Guys and Girls,

I'm trying to read from a file

The file contains call numbers, a variable amount.

The call numbers come after an identifier NOTF

I then want to put all the data after NOTF into a file, but call the file
the name of the unique call number.

so:

while (file not empty)
read (input file) // line by line
if (input file) = NOFT
create file (name of preceding call number)
put (data after NOTF)
until (next NOTF)
}
}
}

Sorry for the VERY bad psuedo code.

Regards,

Darren

Pseudo-code is just an outline, right? And where it all starts....



assuming the line looks like this, more-or-less:

NOTF 12334455 xxxxx xxxxx xxxxxxx xxxxxx xxxxx xxxxx



#!/bin/sh

if [ -s file ] ; then

while read line ; do

name=`echo $line | sed -n 's/\(^NOTF *\)\([0-9][0-9]*\)\( *.*\)/\2/p'`
info=`echo $line | sed -n 's/\(^NOTF *\)\([0-9][0-9]*\)\( *.*\)/\3p'`

echo "$info" > /dir/dir/$name

done < file

fi

exit 0


:) Sorry. Couldn't resist.

C is making me feel dumb at present, and this cheered me up. Now where did
that banana go?
 
D

Darren Stribling

thanks so much for that alan. i was looking at SED last night - but ended up
eating too much pizza instead!

i would like to run this on windows - i guess i could use a port of sed
instead?

Alan Connor said:
Hi Guys and Girls,

I'm trying to read from a file

The file contains call numbers, a variable amount.

The call numbers come after an identifier NOTF

I then want to put all the data after NOTF into a file, but call the file
the name of the unique call number.

so:

while (file not empty)
read (input file) // line by line
if (input file) = NOFT
create file (name of preceding call number)
put (data after NOTF)
until (next NOTF)
}
}
}

Sorry for the VERY bad psuedo code.

Regards,

Darren

Pseudo-code is just an outline, right? And where it all starts....



assuming the line looks like this, more-or-less:

NOTF 12334455 xxxxx xxxxx xxxxxxx xxxxxx xxxxx xxxxx



#!/bin/sh

if [ -s file ] ; then

while read line ; do

name=`echo $line | sed -n 's/\(^NOTF *\)\([0-9][0-9]*\)\( *.*\)/\2/p'`
info=`echo $line | sed -n 's/\(^NOTF *\)\([0-9][0-9]*\)\( *.*\)/\3p'`

echo "$info" > /dir/dir/$name

done < file

fi

exit 0


:) Sorry. Couldn't resist.

C is making me feel dumb at present, and this cheered me up. Now where did
that banana go?
 
E

Ed Morton

Darren said:
thanks so much for that alan. i was looking at SED last night - but ended up
eating too much pizza instead!

i would like to run this on windows - i guess i could use a port of sed
instead?

Note for later: "after" => succeeding.

"preceeding"? You described "succeeding" in your text above.

Note: implies there's a second NOTF on the same line to terminate the
input. OR do you mean to say that the data can actually span multiple
lines and you want to keep reading between NOTFs?


He didn't say that NOTF would be the first thing on the line and he said
there was a second NOTF to terminate the data.

name=`echo $line | sed -n 's/\(^NOTF *\)\([0-9][0-9]*\)\( *.*\)/\2/p'`
info=`echo $line | sed -n 's/\(^NOTF *\)\([0-9][0-9]*\)\( *.*\)/\3p'`

These won't work if the input data is really:

abcd NOTF 1234 def ghi NOTF klm nop

and he wants "1234" for "name" and "def ghi" for "info" which his text
implies. Also, if he really wants to name the file based on the
PRECEEDING call number and/or the data can span multiple lines, that
requires a little different approach.

OP - if you want a C solution, attempt it and post the attempt here. If
you want a shell solution, try posting to comp.unix.shell and take a
look at www.cygwin.org for a UNIX-like environment for Windows.

Ed.
 
C

CBFalconer

Darren said:
thanks so much for that alan. i was looking at SED last night -
but ended up eating too much pizza instead!

i would like to run this on windows - i guess i could use a port
of sed instead?

Your chances of getting reasonable suggestions would be greatly
enhanced by refraining from top-posting. As it is the thread is a
completely illegible jumble.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,091
Messages
2,570,604
Members
47,223
Latest member
smithjens316

Latest Threads

Top