extract numbers from a word


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting extract numbers from a word
# 8  
Old 03-26-2009
Get only dates and hours from each line

Hi all,

I have many lines with several date and hour format within each line and I need to extract only the dates and hours for each line
In the same order.

Example:

Today 090326 she left the office at 20:00, and tomorrow 20090327 will come at 08:00-->Result=090326 20:00 20090327 08:00
I estudied yesterday 25/03/2009, 1524 and I´ll continue tomorrow 27032009 at 17:30-->Result =25/03/2009 1524 27032009 17:30

If I use grep with something like
Code:
grep [0-9] [0-9] [0-9] [0-9]2009 infile
or
grep 09[0-9] [0-9] [0-9] [0-9] [0-9] infile

I don't get only the dates and hours, only some lines that match with that.

May somebody help me doing a filter in order to extract dates and hours in desired format?

Many thanks in advance
# 9  
Old 03-26-2009
Code:
$ perl -ne 's/[^\: \/\d\n]//g; print "Result=" . join(" ", split), "\n"' file
Result=090326 20:00 20090327 08:00
Result=25/03/2009 1524 27032009 17:30


Last edited by rikxik; 03-26-2009 at 05:07 AM..
# 10  
Old 03-26-2009
HI,


Code:
$ echo "Today 090326 she left the office at 20:00, and tomorrow 20090327 will come at 08:00" |sed 's/[a-z]//g'|sed 's/[A-Z]//g'
 090326      20:00,   20090327    08:00

Thanks
SHa
# 11  
Old 03-26-2009
Hi Shahul and rikxik,

I´ve tryed your solutions and works very nice, only some little thing to fix, because I forget that are some lines that contains some other numbers that aren´t of interest for me, for example:

If the text is like
Day_01 090326 she left the office at 20:00, and tomorrow 20090327 will come at 08:00

your codes show 01 090326 20:00, 20090327 08:00

How to eliminate the numbers of 2 or 1 digit like in the word "Day_01" to obtain only dates and hours?

Day_01 090326 she left the office at 20:00, and tomorrow 20090327 will come at 08:00 -->Result 090326 20:00 20090327 08:00. (without 01)

I´ve tryed a little change follow the sed example with

Code:
cat file sed s/[0-9][0-9]//g

but this eliminates all numbers, including dates and hours.

Thanks again.
# 12  
Old 03-26-2009
Hammer & Screwdriver Another way of approaching

Code:
> sample="Day_01 090326 she left the office at 20:00, and tomorrow 20090327 will come at 08:00 "
> sample2=`echo $sample | tr " " "\n" | egrep -e "[0-9][0-9][0-9:]" | tr "\n" " " | tr -d ","`
> echo $sample2
090326 20:00 20090327 08:00

# 13  
Old 03-27-2009
Hope this also can do that..

Code:
echo "Day_01 090326 she left the office at 20:00, and tomorrow 20090327 will come at 08:00"|sed 's/[a-z]//g'|sed 's/[A-Z]_\([0-9]\{2\}\)//g'
 090326      20:00,   20090327    08:00

Thanks
Sha
# 14  
Old 03-28-2009
Thanks for your help, I´m still receiving some number (one or two digits) that I´d like to delete (in red below)

02 2 25032009 1739 25032009 1715 25032009 40
02 2 25032009 1715 25032009 1737
01 25032009 1635 25032009 1732
51 1 13 01 1 25032009 1700 25032009 1642 25032009 1709
51 1 13 01 1 25032009 1642 25032009 1706

I´ve tryed with

Code:
 
cat Infile | sed 's/[A-Za-z:_,-.=()\/]//g'

Please an advice with the correct regex to get this, eliminate the patterns of one or 2 digits only and to sort in columns the result.

Best regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Substr with % - extract numbers only

# cat /etc/redhat-release Red Hat Enterprise Linux Server release 7.5 (Maipo) I have this script that will monitor filesystems and send me e-amil alerts. #! /bin/ksh DIST_LIST=monitor@...com WORKDIR=/home/monitor WARNLEVEL=90 MAIL_SUBJ="filesystems monitor on "$(hostname) ... (3 Replies)
Discussion started by: danielshell
3 Replies

2. Shell Programming and Scripting

Extract numbers from file name-how to ?

Hello friends,I am new to Unix programming. how do I achieve the following in Unix shell script (I am running ksh on AIX) extract the number from name of file? My file format is like "LongFileName-1234.020614-221030.txt" now I want to extract value which is between (-) hyphen and (.) dot... (4 Replies)
Discussion started by: f150
4 Replies

3. Shell Programming and Scripting

Put numbers after word

Hello I have an file with this content -------------------------------------------- timer one timer two timer three timer four timer five timer six timer seven ------------------------------------------- And I want the following output.... (4 Replies)
Discussion started by: thailand
4 Replies

4. Shell Programming and Scripting

extract whole thing in word, leaving behind last word. - perl

Hi, i've a string /u/user/DTE/T_LOGS/20110622_011532_TEST_11_HD_120/HD/TESi T_11_HD_120/hd-12 i need to get string, like /u/user/DTE/T_LOGS/20110622_011532_TEST_11_HD_120/HD the words from HD should get deleted, i need only a string till HD, i dont want to use any built in... (4 Replies)
Discussion started by: asak
4 Replies

5. Shell Programming and Scripting

Summing numbers after specific word

Hi all, Looking for suggestions on a better way to sum numbers in a key value pair formated file. What I have works but seems really clunky to me. Any suggestions would be greatly appreciated. cat test.txt | perl -ne 'm/(M=)(\d+\.?\d?\d?)/ && print "$2\n"' | awk '{ sum+=$1} END {printf... (7 Replies)
Discussion started by: cgol
7 Replies

6. UNIX for Dummies Questions & Answers

Extract numbers from .txt file

I need to extract all the p-value numbers and the rho numbers from a .txt file and write them as coma separated values in a new file. Ideally I would get two files in the end, one for p- values and one for rho. Any suggestions? I appreciate your help!!! The .txt file looks essentially like this... (5 Replies)
Discussion started by: eggali
5 Replies

7. Shell Programming and Scripting

find the last word with all numbers?

Hi, I was trying to extract the last word with all numbers using awk. like the below example. I am looking for a pattern using awk. desired result: (13 Replies)
Discussion started by: hitmansilentass
13 Replies

8. Shell Programming and Scripting

awk fetch numbers after the word

Hi, I would want to fetch all the numbers after a word the number of characters could very. how can I do that? below is the example of the data and the expected output sample data 03 xxxx occurs 1090 times. 04 aslkja occurs 10 times. I would want to fetch 10 & 1090 separately. (13 Replies)
Discussion started by: ahmedwaseem2000
13 Replies

9. Shell Programming and Scripting

BASH. Need to extract some numbers and take the average

Hey all, I ran some simulations, of which the output is 100s of files. I've used grep to extract the vital information needed from the files. This has made my task somewhat easier. But I still need to perform some mathematical calculations (average and geometrical average) on the results of the... (9 Replies)
Discussion started by: slackjack
9 Replies

10. Shell Programming and Scripting

Extract numbers below words with awk

Hi all, Please some help over here. I have a Sales.txt file containing info in blocks for every sold product in the pattern showed below (only for 2 products). NEW BLOCK SALE DATA PRODUCT SERIAL 79833269999 146701011945004 .Some other data .Some... (17 Replies)
Discussion started by: cgkmal
17 Replies
Login or Register to Ask a Question