- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- ksh script function extract line number
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2003 04:25 AM
07-04-2003 04:25 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2003 04:39 AM
07-04-2003 04:39 AM
Re: ksh script function extract line number
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2003 04:53 AM
07-04-2003 04:53 AM
Re: ksh script function extract line number
awk ' /searchstring/ { print NR}' filename
It will print the linenumber which contains the searchstring in filename
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2003 05:01 AM
07-04-2003 05:01 AM
Re: ksh script function extract line number
Thanks for the answer but that did no solve the problem.
Franky
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2003 05:07 AM
07-04-2003 05:07 AM
Re: ksh script function extract line number
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2003 05:09 AM
07-04-2003 05:09 AM
Re: ksh script function extract line number
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2003 05:09 AM
07-04-2003 05:09 AM
Re: ksh script function extract line number
I forget it, at the end of script echo the In varible to see your content.
echo $In
Bye
Frank.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2003 05:09 AM
07-04-2003 05:09 AM
Re: ksh script function extract line number
I forget it, at the end of script echo the In varible to see its contents
echo $In
Bye
Frank.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2003 05:21 AM
07-04-2003 05:21 AM
Re: ksh script function extract line number
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2003 05:25 AM
07-04-2003 05:25 AM
Re: ksh script function extract line number
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2003 05:28 AM
07-04-2003 05:28 AM
Re: ksh script function extract line number
nl your_file|grep -i "your string"
OR
cat file|nl|grep "your string"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2003 05:32 AM
07-04-2003 05:32 AM
Re: ksh script function extract line number
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2003 05:33 AM
07-04-2003 05:33 AM
Re: ksh script function extract line number
can you please post:
- actual script
- example of the content of the file
Just to clarify...
Massimo
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2003 05:35 AM
07-04-2003 05:35 AM
Re: ksh script function extract line number
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2003 05:37 AM
07-04-2003 05:37 AM
Re: ksh script function extract line number
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2003 05:41 AM
07-04-2003 05:41 AM
Re: ksh script function extract line number
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2003 06:14 AM
07-04-2003 06:14 AM
Re: ksh script function extract line number
This script worked for me.
#/bin/ksh
function myFunc {
awk ' /'$1'/{ print NR}' myfile
}
ln=`myFunc "aa" `
echo $ln
~
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2003 06:19 AM
07-04-2003 06:19 AM
Re: ksh script function extract line number
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2003 06:21 AM
07-04-2003 06:21 AM
Re: ksh script function extract line number
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2003 06:27 AM
07-04-2003 06:27 AM
Re: ksh script function extract line number
Similar result with your last hint :
syntax error at line 11 : `(' unexpected
Regards,
Franky
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2003 06:31 AM
07-04-2003 06:31 AM
Re: ksh script function extract line number
you had the same result, removing the () from the function definition ?
Massimo
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2003 06:36 AM
07-04-2003 06:36 AM
Re: ksh script function extract line number
#! /bin/ksh
function getLineNr {
# $1 search string
awk '/'$1'/{print NR}' myfile
}
ln=`getLineNr "Statistics"`
echo $ln
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2003 06:39 AM
07-04-2003 06:39 AM
Re: ksh script function extract line number
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2003 06:44 AM
07-04-2003 06:44 AM
Re: ksh script function extract line number
I added the extra space and yes, this works for the "Statistics" string.
So, 'function myfunction
Franky
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2003 06:46 AM
07-04-2003 06:46 AM
Re: ksh script function extract line number
"Statistics \( 2K\)"
Massimo