Operating System - HP-UX
1832690 Members
2750 Online
110043 Solutions
New Discussion

ksh script function extract line number

 
SOLVED
Go to solution
Franky Leeuwerck_1
Regular Advisor

Re: ksh script function extract line number

Current status :
So far the proposals work for "Statistics" but not for "Statistics ( 2K)".

Franky
Franky Leeuwerck_1
Regular Advisor

Re: ksh script function extract line number

Massimo,

I rememberd your earlier remark and had already tried this with the similar error :

syntax error The source line is 1.
The error context is
>>> /Statistics <<<
awk: Quitting
The source line is 1.

Franky
Dagmar Boelen
Frequent Advisor

Re: ksh script function extract line number

Hi Franky,

I got the same error on my workstation. The script work however worked fine!

#! /bin/ksh
function getLineNr {
# $1 search string
awk '/'$1'/{print NR}' myfile
}
TEST="Statistics ( 2K)"
ln=`getLineNr $TEST`

Dagmar Boelen
Frequent Advisor

Re: ksh script function extract line number

The error you mentioned only occurs when you parse a searchstring to the function which contains a space in it.

This means that a search for Statistics(2K) would work but that a search for Statistics (2K) would fail. The script I posted which uses parsing a $TEST instead of "Statistics (2K)" works however
Franky Leeuwerck_1
Regular Advisor

Re: ksh script function extract line number

Dagmar

With your ' $TEST ' proposal, I can run the script without errors but it returns a wrong result.

I need line number. In the test it returns 3 line numbers, eg for the following lines :

! Statistics------------------------------------------------------------------
! Statistics ( 2K) -----------------------------------------------------------
! Statistics ( 4K) -----------------------------------------------------------

Franky
Franky Leeuwerck_1
Regular Advisor

Re: ksh script function extract line number

For Massimo :

I combined your escaping remark with the $TEST remark from Dagmar.

$TEST="Statistics \( 2K\)"

The script runs without errors but with same wrong results.

Franky
Massimo Bianchi
Honored Contributor

Re: ksh script function extract line number

Well,
and try to escape also the space :)

Massimo

Massimo Bianchi
Honored Contributor

Re: ksh script function extract line number

Idea:


you get and error because the expansion lead to

getliner Statistics (2k)

and your function gets only the "statistics part.

Massimo

Ian Lochray
Respected Contributor

Re: ksh script function extract line number

How about a different approach ...
#! /bin/ksh
getLineNr () {
# $1 search string
grep -n "$@" monitor_dmfcache.tmp | sed -e 's/:.*//'
}
ln=`getLineNr "Statistics ( 2K)"`
echo $ln
Franky Leeuwerck_1
Regular Advisor

Re: ksh script function extract line number

Massimo,

Escaping also the second space give us the same (wrong) result.

Escaping the first space give us an error :

syntax error The source line is 1.
The error context is
/Statistics\/{ print >>> NR} <<<
awk: Quitting
The source line is 1.


Franky
curt larson_1
Honored Contributor

Re: ksh script function extract line number

So far the proposals work for "Statistics" but not for "Statistics ( 2K)".

put double quotes around $1

awk '/'"$1"'/{print NR}' myfile

need line number. In the test it returns 3 line numbers

add a "-" to the end of your seach string, i.e. Statistics-
curt larson_1
Honored Contributor

Re: ksh script function extract line number

also it would be better if you used

ln=$(getLineNR "Statistics ( 2K)")

instead of

ln=`getLineNr "Statistics ( 2K)"`
Franky Leeuwerck_1
Regular Advisor

Re: ksh script function extract line number

Ian,

Nice try but got the following errors :
grep: can't open \(
grep: can't open 2K\)

Franky
Dagmar Boelen
Frequent Advisor

Re: ksh script function extract line number

Hi Franky,

This one outputs only one line (the correct one)!!!

#! /bin/ksh
function getLineNr {
# $1 search string
awk '/'$1'/{print NR}' myfile
}
TEST="Statistics/&&/(2K)"
ln=`getLineNr $TEST`

echo $ln
Franky Leeuwerck_1
Regular Advisor

Re: ksh script function extract line number

Curt,

Your first remark did not gave other results.
The second one return in similar errors.

Thanks anyhow for helping me.

Franky
Dagmar Boelen
Frequent Advisor
Solution

Re: ksh script function extract line number

Hi,

The script below is even better. It wil stop after it has printed the first line number where Statistics (2k) can be found. The previous would print 2 lines if Statistics (2K) occurs two time in the file which we analyze.

#! /bin/ksh
function getLineNr {
# $1 search string
awk '/'$1'/{print NR;exit 1}' myfile
}
TEST="Statistics/&&/(2K)"
ln=`getLineNr $TEST`

echo $ln
~
Massimo Bianchi
Honored Contributor

Re: ksh script function extract line number

May be regular expression can help:

Modifying Ian's answer

#! /bin/ksh
getLineNr () {
# $1 search string

egrep -n "$1" monitor_dmfcache.tmp | awk -F: {print $1 }

}
ln=`getLineNr "Statistics.*2K"`
echo $ln


Massimo

curt larson_1
Honored Contributor

Re: ksh script function extract line number

and your awk would probably be better if written like this

awk -v string="$1" '{
if ( $0 ~ string ) print NR;
}' yourFile
Franky Leeuwerck_1
Regular Advisor

Re: ksh script function extract line number

Hello everyone,

I am trying out every one's proposals in FIFO and Dagmar has given me the solution that worked first.


Dagmar,
Good job.
can you explain to me the /&&/ stuff ?
I might need it for other strings.

To the other participants : thanks a many many many lot for your help. This was exciting.
I learned lot.

I will review the other suggestions also (and assign points if usefull).

Again thanks a lot.

Franky
Dagmar Boelen
Frequent Advisor

Re: ksh script function extract line number

/&&/ means match statistics and 2K. Unfortunatly this is not exactly what you want... It wil also match (2K) Statistics for example
Franky Leeuwerck_1
Regular Advisor

Re: ksh script function extract line number

Ok Dagmar,

Thanks for the update.


Franky