First number sequence from string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting First number sequence from string
# 1  
Old 09-22-2011
First number sequence from string

Hi,

I have a string like:

DBMS stats (Number Used | Percentage of total): 10 | 1.00%

I have a sed command to extract numbers from this string:
Code:
sed "s/[^0-9]//g;s/^$/-1/;"

Output: 10100

However what I want the sed command to return is only the first number(regardless of its size) i.e. 10

Thanks in advance.

Last edited by Franklin52; 09-22-2011 at 08:33 AM.. Reason: Please use code tags, thank you
# 2  
Old 09-22-2011
Hi mccartj5,

Try:
Code:
$ cat infile
DBMS stats (Number Used | Percentage of total): 10 | 1.00%
$ sed 's/^[^0-9]*\([0-9]\+\).*$/\1/' infile
10

Regards,
Birei
# 3  
Old 09-22-2011
Code:
$ nawk -F"[:|]" '{print $3}' infile

This User Gave Thanks to jayan_jay For This Post:
# 4  
Old 09-22-2011
Quote:
Originally Posted by birei
Hi mccartj5,

Try:
Code:
$ cat infile
DBMS stats (Number Used | Percentage of total): 10 | 1.00%
$ sed 's/^[^0-9]*\([0-9]\+\).*$/\1/' infile
10

Regards,
Birei
Hi birei,

Thanks for the quick reply.

As my file can contain several lines of input I only want to evaluate the last line. I have tried the following with your suggestion but it brings me back the full string:

tail -1 infile | sed 's/^[^0-9]*\([0-9]\+\).*/\1/'

output: DBMS stats (Number Used | Percentage of total): 10 | 1.00%

Thanks,
mccartj5

---------- Post updated at 11:47 AM ---------- Previous update was at 11:39 AM ----------

Quote:
Originally Posted by jayan_jay
Code:
$ nawk -F"[:|]" '{print $3}' infile

Spot on jj. I see what you're doing there, looks simple when pointed out Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need append sequence number

Hi, Need to add sequnce number to one of the csv file and please find below actual requirement. Input file ABC,500 XXQ,700 ADF,400, ART,200 Out put file should be 1,ABC,500 2,XXQ,700 3,ADF,400, 4,ART,200 (3 Replies)
Discussion started by: siva83
3 Replies

2. Shell Programming and Scripting

Need Help in adding sequence number to a file

Hi All , I have a file which contains data(comma separated) in below format : 500,Sourav ,kolkata ,8745775020,700091 505,ram,delhi ,9875645874,600032 510 ,madhu ,mumbai ,5698756430 ,500042 515 ,ramesh ,blore ,8769045601 ,400092 I want to add unique sequence number at the start of each... (7 Replies)
Discussion started by: STCET22
7 Replies

3. Shell Programming and Scripting

Hex number sequence help

Need some help doing this ... with awk maybe Input 0DF6 0DF7 0DF8 0DF9 0DFA 0DFB 0DFC 0DFD 0DFF 0E00 0E01 0E02 0E03 0E04 0E05 0E06 (11 Replies)
Discussion started by: greycells
11 Replies

4. Shell Programming and Scripting

find common entries and match the number with long sequence and cut that sequence in output

Hi all, I have a file like this ID 3BP5L_HUMAN Reviewed; 393 AA. AC Q7L8J4; Q96FI5; Q9BQH8; Q9C0E3; DT 05-FEB-2008, integrated into UniProtKB/Swiss-Prot. DT 05-JUL-2004, sequence version 1. DT 05-SEP-2012, entry version 71. FT COILED 59 140 ... (1 Reply)
Discussion started by: manigrover
1 Replies

5. Shell Programming and Scripting

How to take the missing sequence Number?

Am using unix aix KSH... I have the files called MMRR0106.DAT MMRR0206.DAT MMRR0406.DAT MMRR0506.DAT MMRR0806.DAT .... ... MMRR3006.DAT MMRR0207.DAT These files are in one dircetory /venky ? I want the output like this ? Missing files are : MMRR0306.DAT MMRR0606.DAT... (7 Replies)
Discussion started by: Venkatesh1
7 Replies

6. Shell Programming and Scripting

Perl : print the sequence number without missing number

Dear Perl users, I need your help to solve my problem below. I want to print the sequence number without missing number within the range. E.g. my sequence number : 1 2 3 4 5 6 7 8 11 12 13 14 my desired output: 1 -8 , 11-14 my code below but still problem with the result: 1 - 14 1 -... (2 Replies)
Discussion started by: mandai
2 Replies

7. UNIX for Dummies Questions & Answers

Adding Sequence Number to file

Hi All, I need to create a script which checks for a particular file for eg.kumar1.txt. If kumar1.txt is already exist the script should increment the file name as kumar2.txt and so on. Please Advise. Thanks & Regards, Kumar66 (2 Replies)
Discussion started by: kumar66
2 Replies

8. Shell Programming and Scripting

Changing the sequence number

Hi, I have a data as follow: 1 400 2 239 3 871 4 219 5 543 6 ... 7 ... .. ... .. ... 99 818 100 991 I want to replace the sequence number (column 1) that start from 150. The output should like this: 150 400 151 239 (3 Replies)
Discussion started by: nica
3 Replies

9. Shell Programming and Scripting

Adding a sequence number within a file

Can someone please help. I need to add a sequence number to the start of each line of a file. It needs to be 6 characters long. ie 000001 next line starts 000002 etc. (4 Replies)
Discussion started by: Dolph
4 Replies

10. UNIX for Dummies Questions & Answers

sequence number checking

Hi there, I'm wanting to produce a shell script that will check through some file names and identify a skip in sequence (four digit seq num in file name). I have played on the idea of havng a file that has a sorted list of file names which I can read line at a time and cut out the sequence... (1 Reply)
Discussion started by: nhatch
1 Replies
Login or Register to Ask a Question