extract digits from a string in unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract digits from a string in unix
# 1  
Old 08-03-2010
extract digits from a string in unix

Hi all,

i have such string stored in a variable

var1 = 00000120

i want the o/p

var1 = 120

is it possible to have such o/p in ksh/bash ...

thanx in advance for the help

sonu
# 2  
Old 08-03-2010
Code:
var1=0000120
echo $var1 | sed 's/^0\+//g'

# 3  
Old 08-03-2010
Quote:
echo "0000000120" | awk '{print $1 + 0}'
# 4  
Old 08-03-2010
Quote:
Originally Posted by pravin27
Code:
var1=0000120
echo $var1 | sed 's/^0\+//g'

The extended metacharacter "+" is not portable with all sed versions.

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How can I extract digits at the end of a string in UNIX shell scripting?

How can I extract digits at the end of a string in UNIX shell scripting or perl? cat file.txt abc_d123_4567.txt A246_B789.txt B123cc099.txt a123_B234-012.txt a13.txt What can I do here? Many thanks. cat file.txt | sed "s/.txt$//" | ........ 4567 789 099 012 13 (11 Replies)
Discussion started by: mingch
11 Replies

2. Shell Programming and Scripting

Extract n-digits from string in perl

Hello, I have a log file with logs such as 01/05/2017 10:23:41 : file.log.38: database error, MODE=SINGLE, LEVEL=critical, STATE: 01170255 (mode main how can i use perl to extract the 8-digit number below from the string 01170255 Thanks (7 Replies)
Discussion started by: james2009
7 Replies

3. Shell Programming and Scripting

Extract First and matching word from string in UNIX

Thank you (2 Replies)
Discussion started by: Pratik Majithia
2 Replies

4. Shell Programming and Scripting

Extract a string from another string in UNIX

I have a string string="Please have a nice day and sleep well Replace_12123_31233_32134_12342 Good day" How do i replace "Replace_12123_31233_32134_1234" in the above string.?? Please help. Regards, Qwerty (3 Replies)
Discussion started by: qwertyu
3 Replies

5. Shell Programming and Scripting

awk extract certain digits from file with index substr

I would like to extract a digit from $0 starting 2,30 to 3,99 or 2.30 to 3.99 Can somebody fix this? awk --re-interval '{if($0 ~ /{1}{2}/) {print FILENAME, substr($0,index($0,/{1}{2}/) , 4)}}'input abcdefg sdlfkj 3,29 g. lasdfj alsdfjasl 2.86 gr. slkjds sldkd lskdjfsl sdfkj kdjlksj 3,34 g... (4 Replies)
Discussion started by: sdf
4 Replies

6. Shell Programming and Scripting

awk search between 2 digits a string

I would like to search between two a string. I thought this would be easy. The is always at the beginning of a line. The code: gawk '/^/{d=$1},/searchstring/,/^(d+1)/' or gawk '/^/,/searchstring/,/^/' did not return the desired result. inputfile.txt 999 some text searchstring some... (6 Replies)
Discussion started by: sdf
6 Replies

7. Shell Programming and Scripting

find the last digits of a string

print out 201 in following string, Please note the chars before 201 are random, no fixed format. ua07app201 (20 Replies)
Discussion started by: honglus
20 Replies

8. Shell Programming and Scripting

Isolate and Extract a Pattern Substring (Digits Only)

Hi guys, I have a text file report generated from egrepping multiple files. The text files themselves are obtianed after many succesive refinements, so they contain already the desired number, but this is surrounded by unwanted characters, newlines, spaces, it is not always at the start of the... (6 Replies)
Discussion started by: netfreighter
6 Replies

9. Shell Programming and Scripting

Extract digits at end of string

I have a string like xxxxxx44. What's the best way to extract the digits (one or more) in a ksh script? Thanks (6 Replies)
Discussion started by: offirc
6 Replies

10. UNIX for Dummies Questions & Answers

to check if a string has only digits

Hi guys, I am not very experienced in writing ksh scripts and I am trying to write a piece of code that indicates if a given string contains only digits and no alphabet (upper or lower case). If i write it my way it would turn out to have a lot of comparisons.. :eek: Thanks a lot in... (3 Replies)
Discussion started by: lakshmikanth
3 Replies
Login or Register to Ask a Question