Scripting Question

Scripting Question

Post by Larry Galle » Mon, 16 Dec 1996 04:00:00



Howdy All,

I have a scripting problem that I feel should be easy to solve, but I am
fairly new to unix scripting -- and -- apparently not very good at it
yet.

I have a script that creates a database of user problems -- 1 per line.
These problems are numbered such that the problem number is "^"
delimited in the first column of the line.  I want to be able to make
queries on each line (problem) of the database and need to know what the
largest problem number is.  Since I sort, this will always be the first
character in the LAST line of the ascii database.

How can I capture the value of this field into a variable w/in my
script.  Currently I am just counting the number of lines to find this
number, but our procedure will change so that numbers will not
necessarily be assigned sequentially (in other words, if the file has 50
lines, line 50 will not necessarily contain problem number 50 -- it
could be less).  BTW, I'm using Korn.

Any help is appreciated.
--

Larry

 
 
 

Scripting Question

Post by Chuck Trane » Tue, 17 Dec 1996 04:00:00


If I got this right...  Your line looks something like this...

^10^ This is a user problem report....

... and you want to parse out the "10"...

If so...
try something like this...

while read line; do
   num=$(cut -d'^' -f1 $line)
done...

The cut command breaks up the $line variable based on a user
defined vairable specified by the -d arg (^ in this case).  The
-f is the field selected.  I believe -f1 will get you what you
want, but it may be -f2.

Cheers
CHT

 
 
 

Scripting Question

Post by Larry Star » Tue, 17 Dec 1996 04:00:00



> Howdy All,

> I have a scripting problem that I feel should be easy to solve, but I am
> fairly new to unix scripting -- and -- apparently not very good at it
> yet.

> I have a script that creates a database of user problems -- 1 per line.
> These problems are numbered such that the problem number is "^"
> delimited in the first column of the line.  I want to be able to make
> queries on each line (problem) of the database and need to know what the
> largest problem number is.  Since I sort, this will always be the first
> character in the LAST line of the ascii database.

> How can I capture the value of this field into a variable w/in my
> script.  Currently I am just counting the number of lines to find this
> number, but our procedure will change so that numbers will not
> necessarily be assigned sequentially (in other words, if the file has 50
> lines, line 50 will not necessarily contain problem number 50 -- it
> could be less).  BTW, I'm using Korn.

If the file is, in fact, sorted so that the last line will always have
he
highest number.  The following should work for you.

        val=`tail -1 <file> | awk -F^ '{print $1}'`

If you prefer cut to awk that will work also!

If I have misread your format and the "^" is not a delimiter but rather
just
indicates that the number starts at the beginning of the line use:

        val=`tail -1 <file> | awk '{print $1}'`

Quote:

> Any help is appreciated.
> --

> Larry

Hope this helps!  
--

================================================================
There are only three sports: bullfighting, motor racing, and
mountaineering; all the restare merely games. - Ernest Hemingway
 
 
 

Scripting Question

Post by David Weintra » Wed, 18 Dec 1996 04:00:00



> Howdy All,

> I have a scripting problem that I feel should be easy to solve, but I am
> fairly new to unix scripting -- and -- apparently not very good at it
> yet.

> I have a script that creates a database of user problems -- 1 per line.
> These problems are numbered such that the problem number is "^"
> delimited in the first column of the line.  I want to be able to make
> queries on each line (problem) of the database and need to know what the
> largest problem number is.  Since I sort, this will always be the first
> character in the LAST line of the ascii database.

> How can I capture the value of this field into a variable w/in my
> script.  Currently I am just counting the number of lines to find this
> number, but our procedure will change so that numbers will not
> necessarily be assigned sequentially (in other words, if the file has 50
> lines, line 50 will not necessarily contain problem number 50 -- it
> could be less).  BTW, I'm using Korn.

> Any help is appreciated.

I would highly recommend you get the Awk/Sed book from O'Rielly and
Associates. Awk is an excellent way to get to the data in your
database. Quick one line Awk scripts can easily get the information
you need.

Assuming each record looks something like this:

^ProblemNumber^Description, etc, etc, etc...

    biggestNumber=$(tail -1 $myFile | awk -F^ '{print $2}')

If your file looks like this

    ProblemNumber^Description, etc, etc, etc...

Try this

    biggestNumber=$(tail -1 $myFile | awk -F^ '{print $1}')

Normal way of separating data is to use the same character between
each field, so your record should look something like this:

    probNum^user^description^date^etc^etc^etc

Notice that the problem number is separated by the user ID with the
"^" character. Notice that all fields are separated by the same
character. This makes it easy to sort and print the information
with the awk, sed, cut, and sort commands.

--
David Weintraub                   _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
System Configuration Management  _/                                  _/
Deutsche Bank, North America    _/ I AM THE GREAT AND POWERFUL OZ*  _/

                              _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
                       (*Pay no attention to the man behind the curtains)

 
 
 

Scripting Question

Post by Joseph McCaughan cont » Wed, 18 Dec 1996 04:00:00


: Howdy All,

: I have a scripting problem that I feel should be easy to solve, but I am
: fairly new to unix scripting -- and -- apparently not very good at it
: yet.

: I have a script that creates a database of user problems -- 1 per line.
: These problems are numbered such that the problem number is "^"
: delimited in the first column of the line.  I want to be able to make
: queries on each line (problem) of the database and need to know what the
: largest problem number is.  Since I sort, this will always be the first
: character in the LAST line of the ascii database.

: How can I capture the value of this field into a variable w/in my
: script.  Currently I am just counting the number of lines to find this
: number, but our procedure will change so that numbers will not
: necessarily be assigned sequentially (in other words, if the file has 50
: lines, line 50 will not necessarily contain problem number 50 -- it
: could be less).  BTW, I'm using Korn.

: Any help is appreciated.
: --

: Larry

Hi Larry,

It would be nice if you would post an example of what the
database looks like (ficticious data of course).  I will guess the
data looks like this:

1^keyboard stuck
2^system crashes
3^system makes funny noise

It would also be good if we knew how many records you have in
the DB.  Assume 1000.

It would also be good if you would change the delimiter
to ":" in place of "^".  The : is standard Unix (/etc/passwd file).

Here is some info.
Assume the DB is called junkdb.

# numeric sort based on first field using ^ field separator.
sort -nt "^" junkdb
1^keyboard stuck
2^system crashes
3^system makes funny noise

# return largest record number.
sort -nt "^" junkdb | awk -F^ '{print $1}' | tail -1
3

# return description of record number 2.
# NOTE: this will fail if you use more than on lime for description.
#             record number  V
sort -nt "^" junkdb | sed -n 2p | awk -F^ '{print $2}'
system crashes

Hope this helps,

--Joe McCaughan

 
 
 

Scripting Question

Post by Adam Lovic » Thu, 19 Dec 1996 04:00:00



writes

Quote:

>I have a script that creates a database of user problems -- 1 per line.
>These problems are numbered such that the problem number is "^"

>How can I capture the value of this field into a variable w/in my
>script.  Currently I am just counting the number of lines to find this
>number, but our procedure will change so that numbers will not
>necessarily be assigned sequentially (in other words, if the file has 50
>lines, line 50 will not necessarily contain problem number 50 -- it
>could be less).  BTW, I'm using Korn.

Awk is usually pretty efficient at this sort of thing.

awk -F^ '{ num = $1 } END { print num }' file

That sets the field separator to ^, stores field 1 from every line, then
prints the last one. To load a variable with that number, simply enclose
the command line in a back-quoted expression, thus:

lastprob=`awk -F^ '{ num = $1 } END { print num }' file`

Hope that helps.
--
Adam Lovick

 
 
 

Scripting Question

Post by Mark Jahn » Sat, 21 Dec 1996 04:00:00



> Howdy All,

> I have a scripting problem that I feel should be easy to solve, but I am
> fairly new to unix scripting -- and -- apparently not very good at it
> yet.

> I have a script that creates a database of user problems -- 1 per line.
> These problems are numbered such that the problem number is "^"
> delimited in the first column of the line.  I want to be able to make
> queries on each line (problem) of the database and need to know what the
> largest problem number is.  Since I sort, this will always be the first
> character in the LAST line of the ascii database.

> How can I capture the value of this field into a variable w/in my
> script.  Currently I am just counting the number of lines to find this
> number, but our procedure will change so that numbers will not
> necessarily be assigned sequentially (in other words, if the file has 50
> lines, line 50 will not necessarily contain problem number 50 -- it
> could be less).  BTW, I'm using Korn.

If there no gaps in the numbering of your problems then the line count
of the file should be equal to the highest problem number.  To count the
number of lines in your file use:
PROBLEM_MAX=`wc -l <datafile> |awk '{print $1}'`
 
 
 

Scripting Question

Post by rchan.. » Sat, 21 Dec 1996 04:00:00



> If there no gaps in the numbering of your problems then the line count
> of the file should be equal to the highest problem number.  To count the
> number of lines in your file use:
> PROBLEM_MAX=`wc -l <datafile> |awk '{print $1}'`

Why not PROBLEM_MAX=`wc -l < $FILE_NAME` so you could save a tiny bitty
process?

--
--------------------------------------------------------
You might be a redneck if you think I speak for NCR
--------------------------------------------------------

 
 
 

Scripting Question

Post by Ramesh Nataraja » Sat, 21 Dec 1996 04:00:00




>> Howdy All,

>> I have a scripting problem that I feel should be easy to solve, but I am
>> fairly new to unix scripting -- and -- apparently not very good at it
>> yet.

>> I have a script that creates a database of user problems -- 1 per line.
>> These problems are numbered such that the problem number is "^"
>> delimited in the first column of the line.  I want to be able to make
>> queries on each line (problem) of the database and need to know what the
>> largest problem number is.  Since I sort, this will always be the first
>> character in the LAST line of the ascii database.

>> How can I capture the value of this field into a variable w/in my
>> script.  Currently I am just counting the number of lines to find this
>> number, but our procedure will change so that numbers will not
>> necessarily be assigned sequentially (in other words, if the file has 50
>> lines, line 50 will not necessarily contain problem number 50 -- it
>> could be less).  BTW, I'm using Korn.

>If there no gaps in the numbering of your problems then the line count
>of the file should be equal to the highest problem number.  To count the
>number of lines in your file use:
>PROBLEM_MAX=`wc -l <datafile> |awk '{print $1}'`

PROBLEM_MAX=`awk '{ if ( $1 > max ) max=$1} END { print max}'<datafile>`
'll work even if you assign numbers outside the range of 0 and 50 for
a 50 line file.

And,
PROBLEM_MAX=`wc -l < <datafile>` is better than the one with piping.

nram.

 
 
 

Scripting Question

Post by Luke E. Thallmay » Sun, 22 Dec 1996 04:00:00



> Howdy All,

> I have a scripting problem that I feel should be easy to solve, but I am
> fairly new to unix scripting -- and -- apparently not very good at it
> yet.

> I have a script that creates a database of user problems -- 1 per line.
> These problems are numbered such that the problem number is "^"
> delimited in the first column of the line.  I want to be able to make
> queries on each line (problem) of the database and need to know what the
> largest problem number is.  Since I sort, this will always be the first
> character in the LAST line of the ascii database.

> How can I capture the value of this field into a variable w/in my
> script.  Currently I am just counting the number of lines to find this
> number, but our procedure will change so that numbers will not
> necessarily be assigned sequentially (in other words, if the file has 50
> lines, line 50 will not necessarily contain problem number 50 -- it
> could be less).  BTW, I'm using Korn.

        I'm not familiar with ksh but couldn't you just use tail to get
the last line of the ascii file and pull out the first field?
Something like:
    LAST_PROB=`tail -1 < data.file | ...
and you could use awk or cut (?) to pull out the field.

----->Luke