Operating System - HP-UX
1832379 Members
3085 Online
110041 Solutions
New Discussion

ksh script function extract line number

 
SOLVED
Go to solution
Franky Leeuwerck_1
Regular Advisor

ksh script function extract line number

Hello,

As a starter, I would need some help on ksh scripting.

I'd like to retrieve the line number of a string in a file by using a function :

function getLineNr {
# parameters : $1 linenr variable
# $2 search string
export $2
$1=`cat -n /tmp/myfile.tmp | grep "$2" | awk '{print $1}'`
}

In the script I call the function as follows :
ln=" "
getLineNr ln "Statistics ( 2K)"

Running the script gives the error :
getLineNr[x]: (: is not an identifier



Thanks for any help.
Franky
45 REPLIES 45
Darren Prior
Honored Contributor

Re: ksh script function extract line number

Hi,

It could be your variable "In" is getting mistaken for part of the "for x in y" structure, try calling it something unique.

regards,

Darren.
Calm down. It's only ones and zeros...
Dagmar Boelen
Frequent Advisor

Re: ksh script function extract line number

try the following line

awk ' /searchstring/ { print NR}' filename

It will print the linenumber which contains the searchstring in filename
Franky Leeuwerck_1
Regular Advisor

Re: ksh script function extract line number

Darren,

Thanks for the answer but that did no solve the problem.

Franky
Francisco J. Soler
Honored Contributor

Re: ksh script function extract line number

Hi Franky,
Don't pass the variable to the funcion, and define correctly the function.
You musn't do cat -n the NR variable of awk stores the record number.

My proposal:

#------------------------
#script

function getLineNr() {
# $1 search string

awk '/'$1'/ {print NR}' /tmp/myfile.tmp
}

In=`getLineNr "Statistics (2K)"`

#end script
#----------------------------


Be carefuly wit the quotation. The scripts works for me.

Frank.
Linux?. Yes, of course.
Franky Leeuwerck_1
Regular Advisor

Re: ksh script function extract line number

Dagmar,

Thanks for your reply.

Change the function statement into your proposal :
function getLineNr {
# parameters : $1 linenr variable
# $2 search string
export $2
$1=`awk '/$2/{ print NR }' /tmp/myfile.tmp`
}


However, this results in exactly the same error message.

Franky
Francisco J. Soler
Honored Contributor

Re: ksh script function extract line number

Hi Franky

I forget it, at the end of script echo the In varible to see your content.

echo $In

Bye
Frank.
Linux?. Yes, of course.
Francisco J. Soler
Honored Contributor

Re: ksh script function extract line number

Hi Franky

I forget it, at the end of script echo the In varible to see its contents

echo $In

Bye
Frank.
Linux?. Yes, of course.
Massimo Bianchi
Honored Contributor

Re: ksh script function extract line number

Hi,
i think your problem is in the name of the file,
because the () are interpreted by the shell expander.

Try
getLineNr ln "Statistics \( 2K\)"

Massimo



Franky Leeuwerck_1
Regular Advisor

Re: ksh script function extract line number

Francesco,

Thanks for your answer but I get
syntax error at line 11 : `(' unexpected

function getLineNr(){
# $1 search string
# ln line number
awk '/'$1'/{print NR}' /tmp/myfile.tmp
}

Omitting the '()' results in :
syntax error at line 14 : `awk' unexpected


Franky
RAC_1
Honored Contributor

Re: ksh script function extract line number

why function? When you can do it as follows.

nl your_file|grep -i "your string"

OR

cat file|nl|grep "your string"
There is no substitute to HARDWORK
Franky Leeuwerck_1
Regular Advisor

Re: ksh script function extract line number

Massimo,

Thanks also for your answer but I event with that in mind I get : syntax error at line 11 : `(' unexpected

In order to make this issue more simple, I changed the search string to "Statistics".

I still get the same error.

Franky

Massimo Bianchi
Honored Contributor

Re: ksh script function extract line number

Hi,

can you please post:
- actual script
- example of the content of the file

Just to clarify...
Massimo
Franky Leeuwerck_1
Regular Advisor

Re: ksh script function extract line number

Hello Anil,

Thanks for your contribution too.

I need the function because I must repeat this procedure a number of times for different strings.

Using a function will make it possible to do future changes in only one place.

Franky
Massimo Bianchi
Honored Contributor

Re: ksh script function extract line number

Hi,
maybe i eat too juch, but checking with a cluster script, you don't have to use the ()
in the definition.

function customer_defined_run_cmds
{
# ADD customer defined run commands.
: # do nothing instruction, because a function must contain some command.

# Attivazione dell'architettura di Control-M
#su - ecsuser -c start_all
#su - ctmuser -c start-ctm
#test_return 51
}

HTH,
Massimo

Franky Leeuwerck_1
Regular Advisor

Re: ksh script function extract line number

Massimo,

This is the current script :

#! /bin/ksh
function getLineNr(){
# $1 search string
awk '/'$1'/{print NR}' /tmp/monitor_dmfcache.tmp
}
ln=`getLineNr "Statistics"`


This is the file :

! Buffer Status: WBWAIT,SHARED
! Buffer Manager Id: 35 Connected servers 2
! CP count: 8 CP index : 0 CP check : 0
! Database cache size: 40 Table cache size: 40
! Statistics------------------------------------------------------------------
! MUTEXWAIT RECLAIM
! 0 0
! CONSISTENCY POINT FLUSHES WRITE BEHIND FLUSHES
! 7 0
! Buffer Cache Configuration ( 2K) -------------------------------------------
! Buffer count: 8432 Bucket count: 16383 Group count: 54 Size: 8
! Free count: 7740 Limit: 250 Modify count: 260 Limit: 6000
! Free group count: 47 Modify group count: 7
! Fixed count: 0 Group fixed count: 0
! Write Behind start limit: 4800, Write Behind end limit: 4000
! Statistics ( 2K) -----------------------------------------------------------
! FIX CALLS HITS CHECK REFRESH READ TOSS
! 835574 807656 9470 0 5487 2664
! UNFIX CALLS DIRTY FORCE WRITE IOWAIT SYNC
! 182342 14145 107113 1221 3 0
! GREADS GWRITES GWAIT GSYNC FREEWAIT FCWAIT
! 19648 0 0 0 0 0
! Buffer Cache Configuration ( 4K) -------------------------------------------
! Buffer count: 8432 Bucket count: 16383 Group count: 54 Size: 8
! Free count: 8000 Limit: 250 Modify count: 0 Limit: 6000
! Free group count: 54 Modify group count: 0
! Fixed count: 0 Group fixed count: 0
! Write Behind start limit: 4800, Write Behind end limit: 4000
! Statistics ( 4K) -----------------------------------------------------------
! FIX CALLS HITS CHECK REFRESH READ TOSS
! 2374 2318 0 0 30 0
! UNFIX CALLS DIRTY FORCE WRITE IOWAIT SYNC
! 214 0 84 0 0 0
! GREADS GWRITES GWAIT GSYNC FREEWAIT FCWAIT
! 26 0 0 0 0 0
Dagmar Boelen
Frequent Advisor

Re: ksh script function extract line number

Hi Frank,

This script worked for me.

#/bin/ksh
function myFunc {
awk ' /'$1'/{ print NR}' myfile

}

ln=`myFunc "aa" `
echo $ln
~
Ian Lochray
Respected Contributor

Re: ksh script function extract line number

If you remove the word "function" from your scripts it should work OK. It did for me.
Dagmar Boelen
Frequent Advisor

Re: ksh script function extract line number

I agree. You can remove the word function or you can remove the (). Both solutions work!!
Franky Leeuwerck_1
Regular Advisor

Re: ksh script function extract line number

Hi Dagmar,

Similar result with your last hint :
syntax error at line 11 : `(' unexpected

Regards,
Franky
Massimo Bianchi
Honored Contributor

Re: ksh script function extract line number

Hi,
you had the same result, removing the () from the function definition ?
Massimo
Dagmar Boelen
Frequent Advisor

Re: ksh script function extract line number

This script work on my system! I remove the () and added extra space between getLineNr and the { on the line 2! otherwise I get the error awk not expected as you mentioned before. Don't forget the extra space between getLineNr and { !!!

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


echo $ln
Franky Leeuwerck_1
Regular Advisor

Re: ksh script function extract line number

Ian, Dagmar, Massimo

The solution of Ian brings us a bit further.
I can run the script without errors and with correct results for the string "Statistics".

However, I want to search on "Statistics ( 2K)" (see file output).

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


Massimo,
You're right about Dagmar's last remark.
syntax error : `awk' unexpected

Franky


Franky Leeuwerck_1
Regular Advisor

Re: ksh script function extract line number

Dagmar,

I added the extra space and yes, this works for the "Statistics" string.

So, 'function myfunction {' brings us to the same point as Ians proposoal.

Franky
Massimo Bianchi
Honored Contributor

Re: ksh script function extract line number

Try again escaping the ():

"Statistics \( 2K\)"

Massimo