Visit Our UNIX and Linux User Community

how to get number char from a string

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to get number char from a string
# 1  
Old 07-16-2008
how to get number char from a string

for example:

i hav a string like :


how I can extract


I know I can

echo "/rmsprd/arch01/rmsprd/rmsprdarch72736.log" | tr "/" " " | awk '{ print $4 }' to get rmsprdarch72736.log
# 2  
Old 07-16-2008
Tools will it always be in same format?

> echo "/rmsprd/arch01/rmsprd/rmsprdarch72736.log" | cut -d"/" -f5 | cut -d"." -f1

if you know it will always be ten characters (rmsprdarch), then
> echo "/rmsprd/arch01/rmsprd/rmsprdarch72736.log" | cut -d"/" -f5 | cut -d"." -f1 | cut -c11-

# 3  
Old 07-16-2008
Something like:

my_num=`echo $string | sed 's/.*[a-z]\([0-9].*\)\..*/\1/'`

# 4  
Old 07-16-2008
If you are using ksh93 you do not need to invoke external utilities like cut or sed. The following will work
$ str="/rmsprd/arch01/rmsprd/rmsprdarch72736.log"
$ print $str
$ print ${str/*([[:print:]])({5}(\d)).log/\2}

# 5  
Old 07-16-2008
echo "/rmsprd/arch01/rmsprd/rmsprdarch72736.log" | cut -d"/" -f5 | cut -d"." -f1|tr -d [:alpha:]

Previous Thread | Next Thread
Test Your Knowledge in Computers #801
Difficulty: Medium
The CSS z-index property specifies the stack order of an element.
True or False?

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Remove char if not a number

I need to write a BASH script that takes a 2 character string and removes the second character if it is not a digit e.g. If the string is numberical value >9 e.g. string1 = '34' then leave string1 = '34'. However if the string is <10 e.g. string1 = '3X' then remove the second char (which... (7 Replies)
Discussion started by: millsy5
7 Replies

2. Shell Programming and Scripting

How count number of char?

hello how can i cont number of char with loop coomand? i dont want to use wc or other special command the script should check all word's char. one by one also a counter can handle the number As noted in other threads started today. This is not the correct forum for homework assignments. ... (2 Replies)
Discussion started by: nimafire
2 Replies

3. Programming

C++ Using open on a string instead of char*

I am using ifstream to open a file using std::fstream::open void open ( const char * filename, ios_base::openmode mode = ios_base::in ); However I want to use a string instead of a char* as follows but having a problem on how to do this string val_ifmodl = “fred.modl” ifstream ifs_modl;... (2 Replies)
Discussion started by: kristinu
2 Replies

4. Programming

PERL \c char in the string

Hi guys, I am stuck up in a situation. I have a SUN box with certain logs which I need to parse to draw a report using Perl. Now, when I load the text file using a perl degugger to see how the text looks like when the first line of the log file is read in a variable. below is the snapshot of... (2 Replies)
Discussion started by: Asteroid
2 Replies

5. Shell Programming and Scripting

To find number of char occur

To find out number of "|" symbol is available in file: Input: a|b|c|d|z Ouput: 4 I am using below set of commands,It is working... Anybody have anyother solution using sed / awk. cnt=`wc -c <1.txt` cnt1=`tr -d "|" <1.txt >c.dat` cnt2=`wc -c <c.dat` outp=`expr $cnt... (19 Replies)
Discussion started by: Jairaj
19 Replies

6. Shell Programming and Scripting

Parsing char string

I am stumped! I need to parse an input parameter to a script that has the form '-Ort'. I basically need 'O', 'r' and 't', i.e. the individual characters in the string parsed. Since there are no delimiters, I don't know how awk could do this. Can someone tell how to do this, this should be a... (5 Replies)
Discussion started by: ALTRUNVRSOFLN
5 Replies

7. Shell Programming and Scripting

last char from a string

i have a script that reads a plain text file. (its a ksh, and i can use bash also) each line of the file is a fullpath of a file. that makes the list huge. i need to add a functionalitie to that script, i have to be able to add /usr/* or /usr/ and with that reference all the files and folders... (6 Replies)
Discussion started by: broli
6 Replies

8. Programming

Compare Char to String

This is actually a c++ question... Basically I am creating a program that asks for five characters. I have a dictionary file containing tons of words no long than five letters long, on a seperate line. I want to be able to take the five inputted letters and compare them to the words in the file... (3 Replies)
Discussion started by: Phobos
3 Replies

9. Shell Programming and Scripting

Number of specific char in a string.

I wish to compute the number of dot chars in a string. Example: VAR="aaaa.bbbbb.cccc" I try the shortest command to solve this test. Thanks in advance for your help. Regards, Giovanni (7 Replies)
Discussion started by: gio123bg
7 Replies

Featured Tech Videos