SED script to extract and print dates

SED script to extract and print dates

Post by Paul Hampso » Tue, 01 Dec 1998 04:00:00



Hi

I've been working on this for awhile and wondered if anyone can help

Anyone written a sed script which can extract dates from an input file and
then print them out. I don't know why but I keep feeling I could find this
useful. I've written it so it will print out the last date on any line but I
can't get it working to print out both.

The script so far is something like this (apologies this is from memory)

sed -n -f sedfile1 /tmp/testfile

Where sedfile1 contains

s/^.*\(..\/..\/..\).*$/\1/p                    # The code ..\/..\/.. is
approx its actually a bit longer
                                                      # to allow other forms
of dates.

Thanks

BFB

 
 
 

SED script to extract and print dates

Post by Michael P. Reill » Tue, 01 Dec 1998 04:00:00


: Hi

: I've been working on this for awhile and wondered if anyone can help

: Anyone written a sed script which can extract dates from an input file and
: then print them out. I don't know why but I keep feeling I could find this
: useful. I've written it so it will print out the last date on any line but I
: can't get it working to print out both.

: The script so far is something like this (apologies this is from memory)

: sed -n -f sedfile1 /tmp/testfile

: Where sedfile1 contains

: s/^.*\(..\/..\/..\).*$/\1/p                    # The code ..\/..\/.. is
: approx its actually a bit longer
:                                                       # to allow other forms
: of dates.

I can't see anything specifically wrong, but without knowing what you
are trying to accomplish.  You say "to print out both" - both of what?
You have the "-n" option in the command-line, so nothing else will
print.  Are you trying to print the unmodified line as well?

One trick that might help you is to replace the regular delimitors of
the "s" command with some character other than "/".
  s%^.*\(../../..\).*$%\1%p
or
  s!^.*\(../../..\).*$%\1%p
This might pick up some errors later.

  -Arcege

 
 
 

SED script to extract and print dates

Post by Icarus Spar » Wed, 02 Dec 1998 04:00:00



Quote:>Anyone written a sed script which can extract dates from an input file and
>then print them out. I don't know why but I keep feeling I could find this
>useful. I've written it so it will print out the last date on any line but I
>can't get it working to print out both.

>The script so far is something like this (apologies this is from memory)

>sed -n -f sedfile1 /tmp/testfile

>Where sedfile1 contains

>s/^.*\(..\/..\/..\).*$/\1/p

Well, sed is doing what you told it to do. The pattern you are asking it
to match is

^.*     i.e. anything starting at the begining of a line
\(../../..\)    two characters, followed by a slash, 2 chars slash 2 chars
.*$     i.e. anthing up to the end of the line.

so for the line

xmas 25/12/98 boxing day 26/12/98 holidays

the ^.* will match up to the space after 'boxing day'.
In order to print out both dates requires rather more work.
The first problem is that 'regular expressions are greedy'!
If you say this often enough, people will think you are very
knowledgeable - until you meet someone who knows perl well and
points out that they have non-greedy versions as well.

So you real need to chop off the rightmost date, and then see if
there are any more dates left. The following code will do this.

s/^/\
/
:loop
s:\(.*\)\n\(.*\)\(../../..\).*:\3 \1\
\2:
t loop
s/\n//
p

but this gets to be a real mess once you have more than one format
of date to process. The correct tool for this problem is 'lex' (or flex
if you are using GNU tools). All you need write is

%%
[1-9][0-9]"/"[1-9][0-9]"/"[0-9][0-9]    { ECHO ; puts(""); }
.|"\n"                                  { ; }

in a file called 'extdates.l', and then type 'make extdates'. Adding more
suitable date patterns is trivial, e.g. to recognise alphabetic dates.

Icarus

 
 
 

1. Extracting date fields using sed and awk

My program generates a date output from which I need to extract the
output. It should be done such that year, month, day, hour and minute
fields should be extracted separately.

The date output is in this format.  2006/5/25-9:00. The month, day and
hour fields should have 2 decimals instead of 1.

I tried different ways in sed and awk to extract them but I am not
getting any results. Can anyone let me know how to do this.

Thanks,
doni

2. Ghostscript and HP LaserJet III

3. . @!$#<extract me>)*&$% using sed script

4. Administration & setup of X-terminals vs Diskless Workstations

5. extract part of date and use in a shell script

6. PHP ASP load balancing

7. . sed script offered, sed script wanted

8. g77 random number generator

9. how do I use the date command in an sed script?

10. date switch to print date from timestamp

11. Calculating yesterday's date using GNU 'date' (was Re: Require a script)

12. . sed script dc greg ubben sed stud

13. sed problem: using sed in a shell script