extracting only numerals from string.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extracting only numerals from string.
# 1  
Old 04-30-2009
extracting only numerals from string.

Hi!!!

i have two files "tushar20090429200000.txt" and "tushar_err20090429200000.txt"

The numeric part here is date and time.
So this part of file keeps changing after every hour.
I want to extract the numeric part from the both file names and compare them whether they are equal or not.


thanks in Advance....Smilie
# 2  
Old 04-30-2009
Try tghis

script.sh

Code:
var1=`echo $1 | sed -e 's/[:alpha:]//g' -e 's/\.//g'`
var2=`echo $2 | sed -e 's/[:alpha:]//g' -e 's/\.//g'`

if [ "$var1" == "$var2" ]; then
echo "Files equal"
else
echo "not equal"
fi

run as
Code:
script.sh tushar20090429200000.txt tushar_err20090429200000.txt


cheers,
Devaraj Takhellambam
# 3  
Old 04-30-2009
no need to call external sed if using bash
Code:
a=tushar20090429200000.txt
b=tushar_err20090429200000.txt
if [  ${a//[a-zA-Z.]/} = ${b//[a-zA-Z._]/} ] then
 ....
fi

# 4  
Old 04-30-2009
I cant run it as a separate script.
i have to use this logic in a script.
Kindly suggest something else in shell or awk.
# 5  
Old 04-30-2009
Quote:
Originally Posted by tushar_tus
I cant run it as a separate script.
i have to use this logic in a script.
Kindly suggest something else in shell or awk.
Try this. Not tested

Code:
awk 'BEGIN{
match($a,/[0-9]+/);v1=substr($a,RSTART,index($a,".")-RSTART)
match($b,/[0-9]+/);v2=substr($b,RSTART,index($b,".")-RSTART)
if ( v1 = v2 )print "Files Match"
print "Files dont match"
}' a="tushar20090429200000.txt" b="tushar_err20090429200000.txt"

cheers,
Devaraj Takhellambam
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

Sort roman numerals

If I use ls to print all the files of a folder, is there a way to sort using roman numerals? I am thinking about a result like: benjamin_I.wmv benjamin_II.wmv benjamin_II.wmv benjamin_III.wmv benjamin_IV.wmv benjamin_V.wmv benjamin_VI.wmv benjamin_VII.wmv benjamin_VIII.wmv... (6 Replies)
Discussion started by: locoroco
6 Replies

3. Shell Programming and Scripting

Extracting particular string in a file and storing matched string in output file

Hi , I have input file and i want to extract below strings <msisdn xmlns="">0492001956</ msisdn> => numaber inside brackets <resCode>3000</resCode> => 3000 needs to be extracted <resMessage>Request time getBalances_PSM.c(37): d out</resMessage></ns2:getBalancesResponse> => the word... (14 Replies)
Discussion started by: sushmab82
14 Replies

4. Shell Programming and Scripting

How to select only those file names whose name contains only numerals.

Hi Guru's, Before writing to this forum I have searched extensively on this forum about my problem. I have to write a shell script which takes out only those file names from the given directory which contains only numbers. For example, In the given directory these files are present: ... (5 Replies)
Discussion started by: spranm
5 Replies

5. UNIX for Dummies Questions & Answers

Deleting lines starting with spaces then non-numerals

I did a search but couldn't find a thread that seemed to answer this but my apologies if it has been answered before. I have some text files and I need to remove any line that does not start with a number (0-9). In actuality every line like this starts with a 'T' (or 't') but there are a... (5 Replies)
Discussion started by: skray
5 Replies

6. UNIX for Dummies Questions & Answers

Using CUT command to get only numerals from a string

I need help to get only the numerals from a string Ex : var1=Nightfox has 2 red apple(s) I need to cut only the numeral 2 and move it to a variable. var2=`$var1 | cut -c 14` the cut by character doesnt work, how to get only the numeral ? (2 Replies)
Discussion started by: happyrain
2 Replies

7. Shell Programming and Scripting

Extracting String

I am trying to extract a hyperlink from a html document using awk. I have managed to output in the format... href="index.html"> where i would like it just to output index.html. Any ideas on how i would do this? Thanks (2 Replies)
Discussion started by: adpe
2 Replies

8. Shell Programming and Scripting

extracting a string

Hi All, I am writing a shell script for which I am stuck with an extraction part. I arrived till extraction of a path of file. Lets take an example. I now have a file which contains following one line: 2348/home/userid/mydir/any_num_dir/myfile.text Now I want to extract only... (2 Replies)
Discussion started by: start_shell
2 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 from a string

How do I extract 5th to 10th characters of string as given below stored in a shell variable. "ab cd ef gh ij kl" How is cut to be used on this? Thanks for any help. (1 Reply)
Discussion started by: preetikate
1 Replies
Login or Register to Ask a Question