The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Using grep to extract line number mskarica Shell Programming and Scripting 8 06-26-2008 02:47 AM
check whether variable number or string rolex.mp UNIX for Dummies Questions & Answers 4 02-22-2007 12:12 AM
extract from string variable into new variables Sniper Pixie UNIX for Dummies Questions & Answers 2 03-03-2006 10:49 AM
Extract emp number from an error file. jagannatha UNIX for Dummies Questions & Answers 2 05-30-2003 09:39 AM
How to extract a portion of a string from the whole string ds_sastry UNIX for Dummies Questions & Answers 2 09-29-2001 10:40 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-18-2008
vitesse vitesse is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 11
Smile How to extract variable number from a String

hi,I am new to shell script,I have String,like this:
Number of rows exported: 5321
the numbe at end could changing,how can I extract this number and assign it to a variable,then use it later in script.
thanks.
  #2 (permalink)  
Old 09-18-2008
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,390
try this.......
Quote:
variable=`echo "Number of rows exported: 5321"|awk -F: '{print $2}'`
  #3 (permalink)  
Old 09-18-2008
vitesse vitesse is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 11
hi,That works great,here is another issue.
The String been extract is comes from another shell command,like 'db2 "export blala"',
how can i combine your solution with first shell command.
thanks.
  #4 (permalink)  
Old 09-18-2008
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,390
can you post the exact output of your
Quote:
'db2 "export blala"'
command....
  #5 (permalink)  
Old 09-18-2008
vitesse vitesse is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 11
first command is
db2 "export to ACTIONS.dat of del select * from ACTIONS"
it will created following message:
SQL3104N The Export utility is beginning to export data to file
"ACTIONS.dat".

SQL3105N The Export utility has finished exporting "238" rows.


Number of rows exported: 238


I use grep get last line,and use you code get '238',I want assign this number to a variable like $Number,then i use another command like
db2 "insert into log (tablename,number) values('ACTIONS',$Number)
to insert this number to a db2 table log

problem is how can i combine first db2 command with the extract commands(grep and awk) to assign this number to that variable $Number
  #6 (permalink)  
Old 09-18-2008
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,390
this will do the trick...
Quote:
Number=`db2 "export to ACTIONS.dat of del select * from ACTIONS" |grep "Number of rows exported" | awk -F: '{print $2}'`
  #7 (permalink)  
Old 09-18-2008
vitesse vitesse is offline
Registered User
  
 

Join Date: Sep 2008
Posts: 11
hi,thanks for reply,still can not make it work,here is several tests i make,hope it give you some idea of problems.
>x=123
>echo $x
123
>x=`db2 "export to ACTIONS.dat of del select * from ACTIONS" |grep "Number of rows exported" | awk -F: '{print $2}'`
>echo $x

>db2 "export to ACTIONS.dat of del select * from ACTIONS" |grep "Number of rows exported" | awk -F: '{print $2}'
238

It is sames that combination clear the variable x,last one print "238",as I am a newbie,I don't know how to make it work.by the way,I am using bash,'>' is command prompt here.thanks
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:00 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0