extracting a number from a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extracting a number from a string
# 1  
Old 06-06-2008
extracting a number from a string

Hi everyone,
I have a string as follow
ts1n65ulpa4096x16m16_130a_ss1p08v125c
i would like to extract 4096 and 16 from string and save it into two variable. but this string could also have the form
ts1n65ulpa32x16m16_130a_ss1p08v125c
therefore the number before "x" could be 2 or 3 digits. i use the following but this code is not able to extract more than one digit before "x"
echo "ts1n65ulpa4096x16_130a_ss1p08v125c" | sed -e 's/^[a-z0-9]*\([0-9][0-9]*x[0-9]*\).*/\1/'
the output is
6x16
which is missing 409 as the number before "x"
could anyone help me with this
# 2  
Old 06-06-2008
Tools Couple of things to try...

Take a look below. I think it shows ways of getting to before the 'x' and the 2 digits after the 'x'. There may be more elegant solutions, but this one appears to work.

Code:
> echo $val1
ts1n65ulpa4096x16m16_130a_ss1p08v125c
> echo $val1 | cut -c11-
4096x16m16_130a_ss1p08v125c
> echo $val1 | cut -c11- | cut -d"x" -f1
4096
> echo $val1 | cut -c11- | cut -d"x" -f2
16m16_130a_ss1p08v125c
> echo $val1 | cut -c11- | cut -d"x" -f2 | cut -c1-2
16

# 3  
Old 06-06-2008
This should gives you the desired output:

Code:
 sed 's/.*[a-zA-Z]\([^a-zA-Z]*\)x\([^a-zA-Z]*\).*/\1x\2/'

Regards
# 4  
Old 06-06-2008
Query ???

i have a file whose contents are
abffjdkjaf
XMLCHECK := abc def ghi
jfdkjfajfjklaj

i want my script to change the file to (_10_0_0 should be appended to each and rest of the file remail as it is )
hfdhfksajhla
XMLCHECK := abc_10_0_0 def_10_0_0 ghi_10_0_0
jfjjdfjlsjakja

Please help me to do this.
value _10_0_0 is not hardcoaded..it has to be given by a variable.

something like
variable =_10_0_0
XMLCHECK := abc$variable def$variable ghi$variable
# 5  
Old 06-06-2008
Code:
echo ts1n65ulpa4096x1024m16_130a_ss1p08v125c | sed 's/.*[^0-9]\([0-9]\{1,\}x[0-9]\{1,\}\).*/\1/'

# 6  
Old 06-06-2008
ketkeep,

Please start your own thread instead of hijacking another ones thread.
Thanks.
# 7  
Old 06-06-2008
thanks for your help guys, my is working fine
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting substring within string between 2 token within the string

Hello. First best wishes for everybody. here is the input file ("$INPUT1") contents : BASH_FUNC_message_begin_script%%=() { local -a L_ARRAY; BASH_FUNC_message_debug%%=() { local -a L_ARRAY; BASH_FUNC_message_end_script%%=() { local -a L_ARRAY; BASH_FUNC_message_error%%=() { local... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Shell Programming and Scripting

Extracting fixed length number from a text file

Hi, I have a text file with sample records as CASE ID: 20170218881083 Original presentment record for ARN not found for Re-presentment I want to extract the 23 digit number from this file. I thought of using grep but initially couldn't extract the required number. However, after... (16 Replies)
Discussion started by: dsid
16 Replies

3. Shell Programming and Scripting

extracting number of rows

if > wc -l data.txt > 100 (meaning there are 100 rows) i want to assgn 100 as n so that if i do >echo n it would give me 100 Thanks (5 Replies)
Discussion started by: johnkim0806
5 Replies

4. Shell Programming and Scripting

extracting Number variable and the following digits.

HI all, I have output of something like this: crab: ExitCodes Summary >>>>>>>>> 12 Jobs with Wrapper Exit Code : 50117 List of jobs: 1-12 See https:///twiki/something/ for Exit Code meaning crab: ExitCodes Summary >>>>>>>>> 5 Jobs with Wrapper Exit Code : 8001 List of... (20 Replies)
Discussion started by: emily
20 Replies

5. Shell Programming and Scripting

Extracting a certain number with grep command

Hello Friends, when I type grep '275' myfile.txt it gives me all the lines including 275 but it also gives others such as 0.8827588 or 1127507. I only want the lines including 275. How can I extract only 275 without other numbers including 275. thanks (2 Replies)
Discussion started by: rpf
2 Replies

6. Shell Programming and Scripting

changing number in bash (number is in form of string)

I have a txt file as database. when i run my program what it does is it ask me for 3 name and stored in the file as name1:name2:name3:1 when u enter 3 name it add those in file as above format and add 1 at the end. I what i want is if i enter same names again it changes that 1 to 2 and so... (3 Replies)
Discussion started by: Learnerabc
3 Replies

7. Shell Programming and Scripting

Extracting N lines match number X of a pattern

Hi All, as the title says I need to extract N lines after match number X of a pattern. e.g. 111 xxx xxx 111 yyy yyy 111 www www 111 zzz zzz I would like to extract the two lines after the second 111 occurrence. I tried with grep but I didn't find any options to do that. Any... (11 Replies)
Discussion started by: f_o_555
11 Replies

8. Shell Programming and Scripting

Extracting a Number from Passage

( Database Connection Information Database server = DB2/AIX6000 8.1 SQL authorization ID = LAKNAR Local database alias = SAMPLE ROWSAFFECTED ------------ 1 1 record(s) selected. DB20000I The SQL command completed successfully. DB20000I The TERMINATE... (1 Reply)
Discussion started by: laknar
1 Replies

9. Shell Programming and Scripting

Extracting a string from one file and searching the same string in other files

Hi, Need to extract a string from one file and search the same in other files. Ex: I have file1 of hundred lines with no delimiters not even space. I have 3 more files. I should get 1 to 10 characters say substring from each line of file1 and search that string in rest of the files and get... (1 Reply)
Discussion started by: mohancrr
1 Replies

10. Shell Programming and Scripting

Extracting 10 digit number from txt files

Hi, Was wondering if you could give me an example of extracting a 10 digit number from 5 txt files using a regular expression (the number is always different ) and storing the numbers in variables Thanks C19 (9 Replies)
Discussion started by: c19h28O2
9 Replies
Login or Register to Ask a Question