How to extract variable number from a String


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to extract variable number from a String
# 1  
Old 09-18-2008
Bug 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  
Old 09-18-2008
try this.......
Quote:
variable=`echo "Number of rows exported: 5321"|awk -F: '{print $2}'`
# 3  
Old 09-18-2008
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  
Old 09-18-2008
can you post the exact output of your
Quote:
'db2 "export blala"'
command....
# 5  
Old 09-18-2008
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  
Old 09-18-2008
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  
Old 09-19-2008
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract number from string.

Hi I am on Sun os. I have data in the below format and I need to grab the number out from the string. O/p needed: ---------- Post updated at 12:39 PM ---------- Previous update was at 12:32 PM ---------- I tried this but I am getting . at the front (14 Replies)
Discussion started by: dsravanam
14 Replies

2. UNIX for Dummies Questions & Answers

Extract variable name in a string

Hi, I'm doing a script to list all scripts called by a "master" script. But I have an issue as there is some variables in the name of the called scripts. Example: % cat master_script.sh ENVIR=PROD VERSION=1.2 /users/maturix/$ENVIR/program_$VERSION.shI would like my script displays a kind... (7 Replies)
Discussion started by: maturix
7 Replies

3. Shell Programming and Scripting

Extract number part from the string in ksh 88

I have to extract number part (Date and timestamp part ) from the following 3 strings AB_XYZA_20130930183017.log AB_DY_XYZA_20130930183017.log AB_GZU_20130930183017.log Output should be 20130930183017 Please help me to get the string like above Thanks (2 Replies)
Discussion started by: smile689
2 Replies

4. Shell Programming and Scripting

how to extract last word and a number from a string

I have the following script (which I made by my self) #!/bin/bash # add a few empty lines to make it more legible # add a date description on each update interval echo "" >> /home/user/DYN_DNS_IP_change.log echo "" >> /home/user/DYN_DNS_IP_change.log echo "" >>... (6 Replies)
Discussion started by: mahirzukic2
6 Replies

5. Shell Programming and Scripting

Extract number that comes after a given string

Hi! I have a file that contains non-regular strings like: SCSGTI:N="$4,0,1,4,34622991111-->RemoteSPC: 1111", NWID=1; SCSGTI:N="$4,0,1,4,34622991211-->RemoteSPC: 1211", NWID=1; SCSGTI:N="$4,0,1,4,*-->RemoteSPC: 2112,Sec:RemoteSPC: 2212", NWID=1; SCSGTI:N="$4,10,1,4,34622999213-->RemoteSPC:... (4 Replies)
Discussion started by: Flavius
4 Replies

6. Shell Programming and Scripting

sub-string extract between variable delimiters

I need to extract certain pieces from a string, wher delimiters may vary. For example A0 B0 C0 12345677 X0 Y0 Z0 A1-B1 C1 12345678 X1 Y0 Z0 A1/B2 C77 12345679 X2 Y0 Z0 I need to get C0 12345677 X0 C1 12345678 X1 C77 12345679 X2 I tried sed, see example below: echo 'A0 B0... (2 Replies)
Discussion started by: migurus
2 Replies

7. Shell Programming and Scripting

Search for string in a file and extract another string to a variable

Hi, guys. I have one question: I need to search for a string in a file, and then extract another string from the file and assign it to a variable. For example: the contents of the file (group) is below: ... ftp:x:23: mail:x:34 ... testing:x:2001 sales:x:2002 development:x:2003 ...... (6 Replies)
Discussion started by: daikeyang
6 Replies

8. UNIX for Dummies Questions & Answers

Using GREP/AWK to extract a number and store it as a variable

Hello, I have a question regarding the awk command. Here is the line I need to grep: 1 F= -.13250138E+03 E0= -.13249556E+03 d E =-.174650E-01 mag= 35.2157 Instead of displaying the number in red I would like to store it as a variable such as X. Is there a way to do this? Thanks for any... (3 Replies)
Discussion started by: modey3
3 Replies

9. UNIX for Dummies Questions & Answers

Using GREP to extract variable following a string

Hello, I'm running calculations and I need to extract a specific number from a output file. So far I've only been able to GREP entire lines containing the string: '1 F=' . I would like to go a step further and extract just the number following '1 F='. The entire line looks like: 1 F=... (10 Replies)
Discussion started by: modey3
10 Replies

10. UNIX for Dummies Questions & Answers

extract from string variable into new variables

I have a variable which consists of a string like this: 001 aaabc 44 a bbb12 How do I extract each substring, delimited by the spaces, into new variables - one for each substring? eg var1 will be 001, var2 will be aaabc, var3 will be 44, var4 will be a, etc? I've come up with this:... (2 Replies)
Discussion started by: Sniper Pixie
2 Replies
Login or Register to Ask a Question