Help on awk and grep command to get the date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help on awk and grep command to get the date
# 8  
Old 06-17-2013
Why use all this temp file?
Change your script to:
Code:
var1=$(sed -n 1p test.sh | awk -F"[][]" '/INFO :/ {print $2}')
var2=$(sed -n '$p' test.sh | awk -F"[][]" '/INFO :/ {print $2}')
diff=$(($(date -d "$var2" +%s) - $(date -d "$var1" +%s)))
echo $diff
11

Give me the complete log and what to select, and I can for sure shorten this more.


Edit:
If you take the first and last line from test.sh, use this
Code:
var1=$(awk -F"[][]" 'NR==1 {print $2}' test.sh ) 
var2=$(awk -F"[][]" '{s=$2} END {print s}' test.sh)
diff=$(($(date -d "$var2" +%s) - $(date -d "$var1" +%s)))

or all in one
Code:
diff=$(($(date -d "$(awk -F"[][]" '{s=$2} END {print s}' test.sh)" +%s) - $(date -d "$(awk -F"[][]" 'NR==1 {print $2}' test.sh )" +%s)))


Last edited by Jotne; 06-17-2013 at 07:28 AM..
This User Gave Thanks to Jotne For This Post:
# 9  
Old 06-17-2013
Code:
sed -n 's/[^[]*\[\([^]]*\)\].*/\1/;1p;$p;' logfile > a.out
< a.out awk -F'[ :]' '
BEGIN{
m["Jan"]=0
m["Feb"]=31
m["Mar"]=59
m["Apr"]=90
m["Mai"]=120
m["Jun"]=151
m["Jul"]=181
m["Aug"]=212
m["Sep"]=243
m["Oct"]=273
m["Nov"]=304
m["Dec"]=334
}
function to_sec(year,mon,day,hour,min,sec){
  return sec+60*(min+60*(hour+24*(day+m[mon]+365*year)))
}
NR==1 {time1=to_sec($7,$2,$3,$4,$5,$6)}
NR==2 {time2=to_sec($7,$2,$3,$4,$5,$6)}
END {print time2-time1, "seconds"}'

You can save the a.out file by doing a pipe
Code:
sed ... |
awk ...

This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

awk command in hp UNIX subtract 30 days automatically from current date without date illegal option

current date command runs well awk -v t="$(date +%Y-%m-%d)" -F "'" '$1 < t' myname.dat subtract 30 days fails awk -v t="$(date --date="-30days" +%Y-%m-%d)" -F "'" '$1 < t' myname.dat awk command in hp unix subtract 30 days automatically from current date without date illegal option error... (20 Replies)
Discussion started by: kmarcus
20 Replies

2. Shell Programming and Scripting

Date in awk command

Hello guys. i have problem to execute the: DT=$(date +"%Y%m%d"); command in awk script. i wrote this script: #bin/bash BEGIN{ FS="" DT=$(date +"%Y%m%d"); FullBackupValue=0; LevelBackupValue=0; ControlBackupValue=0; ArchBackupValue=0; } { if($1 == "full" &&... (4 Replies)
Discussion started by: Ymir
4 Replies

3. Shell Programming and Scripting

Find week of the year for given date using date command inside awk

Hi all, Need an urgent help on the below scenario. script: awk -F"," 'BEGIN { #some variable assignment} { #some calculation and put values in array} END { year=#getting it from array and assume this will be 2014 month=#getting it from array and this will be 05 date=#... (7 Replies)
Discussion started by: vijaidhas
7 Replies

4. Shell Programming and Scripting

Help in using date command inside awk

Hi All, bash-3.2$ autorep -J BOX_NAME% -l0 | grep BOX_NAME| awk -f awkScript.awk sh: -c: line 0: unexpected EOF while looking for matching `"' sh: -c: line 1: syntax error: unexpected end of file BOX_NAME SU 06/21/2013 03:44:03 06/21/2013 07:46:37 0 #My awkfile { ... (3 Replies)
Discussion started by: ddspark
3 Replies

5. Shell Programming and Scripting

awk - append date to output of a command

Hi all; I am running a script:/var/tmp/gcsw -ne | grep "State:2" | wc that gives me output like:80 480 6529 but i need this output as:2013-01-18 13:00 -> 80 480 6529 (1 Reply)
Discussion started by: gc_sw
1 Replies

6. Shell Programming and Scripting

awk command with date

hi, I have one file /home/user/file1 which entry is below cat /home/user/file1 /tmp/test1 /tmp/test2 /tmp/test3 now I want to create a new file with name /home/user/file2 with date as suffix after each entry. like cat /home/user/file2 /tmp/test1_`date "+%Y-%m-%d"`... (5 Replies)
Discussion started by: anshu ranjan
5 Replies

7. Shell Programming and Scripting

grep date with copy command

how can i copy only those files with creation date less then 24 hours. (1 Reply)
Discussion started by: Danish Shakil
1 Replies

8. Shell Programming and Scripting

Using awk with the date command and escape characters

I have a file that is a log file for web traffic. I would like to convert the timestamp in it to unix time or epoch time. I am using the date command in conjunction with awk to try to do this. Just myfile: 28/Aug/1995:00:00:38 1 /pub/atomicbk/catalog/home.gif 813 28/Aug/1995:00:00:38 1... (3 Replies)
Discussion started by: jontjioe
3 Replies

9. Shell Programming and Scripting

Show date/time with tail|grep command

Hi, I have a log file without date/time, and I want that everytime tail|grep find something it displays the date/time and the line. I have tried something like this command but without any luck to display the date/time: tail -F catalina.out | sed "s/^/`date `/" | egrep ... (6 Replies)
Discussion started by: julugu
6 Replies

10. Shell Programming and Scripting

Using awk and current date command

I am running a command to extract data from a database which has different text and dates (ex. 01/03/07, 05/29/08, 06/05/08). Once the file is created I need to grep for all the text with a current date then redirect the sorted data to another file then email the file out. Here is what I have right... (2 Replies)
Discussion started by: wereyou
2 Replies
Login or Register to Ask a Question