Extracting date fields using sed and awk

Extracting date fields using sed and awk

Post by doni » Sat, 27 May 2006 06:12:11



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

 
 
 

Extracting date fields using sed and awk

Post by Torfinn.Otte.. » Sat, 27 May 2006 07:13:22



> 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

Try this one:

echo 2006/5/25-9:00 | \
awk 'gsub(/[/:-]/," "){printf "%i %.2f %.2f %.2f %i\n", $1, $2, $3, $4,
$5}'

Torfinn

 
 
 

Extracting date fields using sed and awk

Post by doni » Sat, 27 May 2006 09:00:44


Hi Torfinn,

I tried but it didnt work.
Here's the output that I got when I tried with ur method. "2006525900
0.000.000.00 0"

 
 
 

Extracting date fields using sed and awk

Post by Torfinn.Otte.. » Sat, 27 May 2006 16:20:33


I believe you are almost there. Observe the single space in the gsub
replacement field and between format specifiers in the print format
string. See the following for hopefully better readability:

awk '
gsub(/[/:-]/,"              "){
printf "%i     %.2f      %.2f     %.2f     %i\n", $1, $2, $3, $4, $5

Quote:}      '

Tested using  gawk 3.1.5 .
 
 
 

Extracting date fields using sed and awk

Post by Michael Tosc » Sat, 27 May 2006 19:05:50



> 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

echo 2006/5/25-9:00 | awk -F '[/:-]' '{print $1,$2,$3,$4,$5}'
2006 5 25 9 00

--

 
 
 

Extracting date fields using sed and awk

Post by doni » Sun, 28 May 2006 01:30:35


Thanks Torfinn and Michael. I tried both of your methods and they work
fine. I like Michael's way of doing the function.
 
 
 

1. cuts fields by date sed, awk ?

Hello everyone,

I have a log file which has many weeks worth of entries. The log
entries
are 3 lines long followed by a space.

<Wed Mar 29  19:14:09 2006>
xxxxxxxxxxxxxxxxxxxxxserver has rebooted.
<END>

<Wed Mar 30 14:18:29 2006>
xxxxxxxxxxxxxxxxxxxxxserver has rebooted.
<END>

What i want is to take only the current dates entries and write them to
a new file

Thanks for the help,

2. Welcome to comp.unix.shell [Frequent posting]

3. Using awk And/Or sed To Edit Fourth Field Of File

4. ssl port 443

5. xchat transparency on KDE

6. securing directories from ftp connections

7. Using awk or cut to print every field after the 11th field

8. awk - extracting field between "'"

9. sed: extracting single field from each line of a CSV file

10. Using date in awk cannot reflect the actual date...

11. Help with awk/sed word extracting