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
# 8  
Old 09-19-2008
I think the problems is that there is a space before number,how can I get rid of this space.thanks!
# 9  
Old 09-19-2008
I've change your command slightly, it's not necessary to use a field separator if you print the 5th field:

Code:
x=`db2 "export to ACTIONS.dat of del select * from ACTIONS" |grep "Number of rows exported" | awk '{print $5}'`

Regards
# 10  
Old 09-19-2008
the space is deleted,but I am still can not assign the number to the variable x.
echo $x gives nothing. what can i do next?
# 11  
Old 09-19-2008
Try to break it down on your command line, and see what happens.
First:
Code:
db2 "export to ACTIONS.dat of del select * from ACTIONS"

Does this produce the expected result?
If no, copy and paste THE EXACT results here.

If yes, proceed to
Code:
db2 "export to ACTIONS.dat of del select * from ACTIONS" |grep "Number of rows exported"

Does this produce the expected result?
If no, copy and paste THE EXACT results of this test AND THE PREVIOUS tests here.

If yes, proceed to
Code:
db2 "export to ACTIONS.dat of del select * from ACTIONS" |grep "Number of rows exported" | awk '{print $1}'

Does this print the first word or column from the results of the previous test?
Yes? Try the next column.
If no, copy and paste THE EXACT results of this test AND THE PREVIOUS tests here.

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

Does this print the second word or column from the results of the previous test?
Yes? Try the next column.
If no, copy and paste THE EXACT results of this test AND THE PREVIOUS tests here.

etc.

Keep checking - you may discover the problem is an extra space or capital letter or something equally simple...
# 12  
Old 09-19-2008
ALL seperate code your mentioned print the correct results.
the code:
db2 "export to ACTIONS.dat of del select * from ACTIONS" |grep "Number of rows exported" | awk '{print $5}'
gets following result:
238
but code:
NUMBER=`db2 "export to ACTIONS.dat of del select * from ACTIONS" |grep "Number of rows exported" | awk '{print $5}'`
generates nothing,when i run echo $NUMBER after this,it print nothing too,even when I assign a value to NUMBER before I run all those script.
# 13  
Old 09-19-2008
Do you echo the variable $Number after the script exits or within the script?
# 14  
Old 09-19-2008
yes,both print nothing.
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