Sed to grep only numbers in string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed to grep only numbers in string
# 1  
Old 11-07-2008
Sed to grep only numbers in string

Hi,

I would like to get only number in the following strings.

var1="Type20"
var2="type 3"
var3="value 2"
var4="Type 1 Datacenter Hall 2"

I would like to extract output as 20 from var1 and 3 from var2,2 from var3 and 1 from var4.

Appreciate any one help asap..

Regards,
Aji
# 2  
Old 11-07-2008
Hammer & Screwdriver What about this solution?

Code:
> echo "Type 20" | tr -d " " | tr -s "[:alpha:]" "~" | cut -d "~" -f2
20

> echo "Type 1 Datecenter Hall 2" | tr -d " " | tr -s "[:alpha:]" "~" | cut -d "~" -f2
1

# 3  
Old 11-07-2008
Nice use of the worm character :P
# 4  
Old 11-07-2008
Code:
$ cat file
var1="Type20"
var2="type 3"
var3="value 2"
var4="Type 1 Datacenter Hall 2"
$ sed 's [^"]*[^0-9]*\([0-9]*\).* \1 ' file
20
3
2
1

# 5  
Old 11-07-2008
Better than everyone..!!

I have found out the solution my self and works fine..thanks for my self ..:P

nimbld1:root:/home/users/thayata/cvs/test # var="Type 3"
nimbld1:root:/home/users/thayata/cvs/test # print $var |tr -d '[A-Za-z]' |sed 's/ //g'
3
nimbld1:root:/home/users/thayata/cvs/test #
# 6  
Old 11-07-2008
Quote:
Originally Posted by ajilesh
I have found out the solution my self and works fine..thanks for my self ..:P
What about var4 ? Your solution will return 12
Take radoulov solution.

Last edited by danmero; 11-07-2008 at 01:48 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Getting string from xml tags using sed and grep

how to get string from tags using sed and grep i try to capture the tags : <groupId>org.test.proj.assent</groupId> <artifactId>mainapp</artifactId> <version>mainapp.1.4</version> <packaging>pom</packaging> <name>main app 1</name> and then from there i guess i will... (7 Replies)
Discussion started by: umen
7 Replies

2. Shell Programming and Scripting

Replace string of a file with a string of another file for matches using grep,sed,awk

I have a file comp.pkglist which mention package version and release . In 'version change' and 'release change' line there are two versions 'old' and 'new' Version Change: --> Release Change: --> cat comp.pkglist Package list: nss-util-devel-3.28.4-1.el6_9.x86_64 Version Change: 3.28.4 -->... (1 Reply)
Discussion started by: Paras Pandey
1 Replies

3. Shell Programming and Scripting

Grep and sed (replace string in patterned lines)

Grep and Sed (replace string in patterned lines) Hi all, I want to grep for "PATTERN" and only if "PATTERN" is in a line, this line shall be used as replacement input e.g. for SED. I don't get it running in one line. NOT RUNNING - just first idea... I don't know how to redirect grep... (2 Replies)
Discussion started by: unknown7
2 Replies

4. Shell Programming and Scripting

Need help please with Grep/Sed command to extract text and numbers from a file

Hello All, I need to extract lines from a file that contains ALPHANUMERIC and the length of Alphanumeric is set to 16. I have pasted the sample of the lines from the text file that I have created. My problem is that sometimes 16 appears in other part of the line. I'm only interested to... (14 Replies)
Discussion started by: mnassiri
14 Replies

5. Shell Programming and Scripting

String: Grep / SED for multy line search

Hi, At first I want to please you to provide the solution with grep/sed if possible. :cool: File looks like: wished result: so I want in a new file BLUE@@RED string from first line like: grep "/folder_start" cs_src > tmp1 string from second line: grep "/main" cs_src... (14 Replies)
Discussion started by: unknown7
14 Replies

6. Shell Programming and Scripting

GREP/SED - get string in a line

Hi, I simply try to get a string in a line but I do smth. wrong. Hopfefully you can help me ;) tried smth like: ggrep -Eo " /folder1/folder2/folder3/* end" get_info_file > temp.file I played a bit around but could not specify the end string command... So this is the... (9 Replies)
Discussion started by: unknown7
9 Replies

7. UNIX for Dummies Questions & Answers

sed/grep string replace question

Hi all, I know this question has probably been answered before, but I am struggling with this problem, even after googling a million pages. In a file named rdmt.conf I need a single character replaced, the number in the line below CUR_OC4J_ID=1 It will always appear after... (3 Replies)
Discussion started by: Mike AAA
3 Replies

8. Shell Programming and Scripting

Like grep -v for a string over 2 lines? Sed?

Hi, I have a log file that I need to monitor as it's being written to, and I want to exclude certain strings from the output. At the moment I'm using ... tail -f LogFileName_`date +%d`.log | egrep -v "First String To Exclude | 2nd string | 3rd string" ...which works OK - but now I need to... (1 Reply)
Discussion started by: jake657
1 Replies

9. Shell Programming and Scripting

Need shell/sed script for grep+string replacement

Hi, Let me explain the situation. There are many files in a directory and its sub-directories that conatin the string pattern "pa". I want to replace all such instances with the pattern "pranavagarwal" doing a grep "pa" `ls` does give me all the instances of the occurence of that... (3 Replies)
Discussion started by: pranavagarwal
3 Replies

10. UNIX for Dummies Questions & Answers

grep a string in a line using sed

Hi, I'm trying to grep a string in a line using sed. My original data looks like this: MRTG$ grep -i "System" $file <H1>Traffic Analysis for 15 -- sERITHC3602.t-mobile.co.uk</H1> <TABLE> <TR><TD>System:</TD> <TD>sERITHC3602 in </TD></TR> <TR><TD>Maintainer:</TD> <TD></TD></TR>... (4 Replies)
Discussion started by: viadisky
4 Replies
Login or Register to Ask a Question