Find a value and the previous timestamp


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find a value and the previous timestamp
# 1  
Old 09-04-2008
Find a value and the previous timestamp

Hi,

I need to do a script that find a specific data in var_5 and also the previous timestamp. This is the file I have:

[2008/09/04 12:46:00]
var_1=data1
var_2=data2
var_3=data3
var_4=data4
var_5=data5
var_6=data6
var_7=data7
RESULT
PRINTOUTS:
6404
var_1=data8
var_2=data9
var_3=data10
var_4=data11
var_5=data12
var_6=data13
var_7=data14
RESULT
PRINTOUTS:
6404
var_1=data15
var_2=data16
var_3=data17
var_4=data18
var_5=data19
var_6=data20
var_7=data21
RESULT
PRINTOUTS:
6404
[2008/09/04 12:51:00]
var_1=data22
var_2=data23
var_3=data24
var_4=data25
var_5=data26
var_6=data27
var_7=data28
RESULT
PRINTOUTS:
6404
var_1=data29
var_2=data30
var_3=data31
var_4=data32
var_5=data33
var_6=data34
var_7=data35
RESULT
PRINTOUTS:
6404

What I want to do is, for exemple, find if the "data33" is in the file and if it is returns the previous timestamp, in this case "[2008/09/04 12:51:00]"

Can someone help me?
# 2  
Old 09-04-2008
Code:
awk '{
   if ($0 ~ /^\[/)
      dt = $0
   if ($0 == "var_5=data33")
      print dt
}' file

# 3  
Old 09-04-2008
Quote:
Originally Posted by shamrock
Code:
awk '{
   if ($0 ~ /^\[/)
      dt = $0
   if ($0 == "var_5=data33")
      print dt
}' file

Thank you a lot man!! this works just like I wanted!! I don't understand what it does, if you can explain..

Anyway, once again thank you!
# 4  
Old 09-05-2008
Now I need something else.. I still want the same ouput but I also want to print de var_7 value.

In the previous example, when looking for data33, should be:

[2008/09/04 12:51:00] data35

I've tried some solutions but none of them works..

Thank you
# 5  
Old 09-05-2008
Code:
awk -F\= '{
   if ($0 ~ /^\[/)
      dt = $0
   if ($0 == "var_5=data33")
      flg++
   if ($0 == "var_7=data35" && flg--)
      print dt, $2
}' file

# 6  
Old 09-05-2008
Code:
while read -r line
do
    case $line in 
        [* ) l=$line;;
        var_5*data33) echo $l;;
    esac
done < file

# 7  
Old 09-05-2008
Quote:
Originally Posted by shamrock
Code:
awk -F\= '{
   if ($0 ~ /^\[/)
      dt = $0
   if ($0 == "var_5=data33")
      flg++
   if ($0 == "var_7=data35" && flg--)
      print dt, $2
}' file

This solution has a problem because for which var_5 value I have a var_7 value and it can or can not be the same from others var_7 along the file. What I want is the var_7 value just after the specific var_5 value.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find files and sort by timestamp

Used below command to get list of files sorted by timestamp find -L . -type f -name '*dat*' | xargs ls -ltrg I want to get only the filenames so I tried adding basename but it doenst work , can some one advise on how to get only file name (1 Reply)
Discussion started by: lalitpct
1 Replies

2. UNIX for Advanced & Expert Users

Find files modified in previous minute only

Hi, How can I get files which are modified only in last minute ? it should not display 2 minutes back filels -la -rw-rw-r-- 1 stuser st 51 Dec 3 09:22 a.csv -rw-rw-r-- 1 stiser st 50 Dec 3 09:25 b.csv -rw-rw-r-- 1 stuser st 53 Dec 3 09:33 c.csv When I run command at 9:34am then I... (7 Replies)
Discussion started by: sbjv
7 Replies

3. Shell Programming and Scripting

Find and delete file previous to some date

Hello All, I have a directory containing of many .dat file, but with different naming conventions. I want to delete files which are created before and on 10th September 2013. I tried this command to see files which are are the files created before 10th September find path/to/file -type... (6 Replies)
Discussion started by: nnani
6 Replies

4. Shell Programming and Scripting

How to append timestamp in the filenames using find?

Hi, How to change the filenames with timestamp in sub folders I have the following code to select the records. find . -type f -name '*pqr*' -ctime 1 -print The following is the example app_root_dir="/`echo $ScriptDir | cut -d'/' -f2`" $app_root_dir/../BadFiles directory uvw.bad... (3 Replies)
Discussion started by: bobbygsk
3 Replies

5. Shell Programming and Scripting

How to find the date of previous day in shell script?

Hi Experts, i am using the below code get the date of previous day. #!/usr/bin/ksh datestamp=`date '+%Y%m%d'` yest=$((datestamp -1)) echo $yest When i execute the code i am getting output as: 20130715 What i am trying here is, based on the date passed i am fetching previus day's... (0 Replies)
Discussion started by: learner24
0 Replies

6. Shell Programming and Scripting

Find logs between two timestamp

Suppose there are log entries like 16 Jul 2012 11:46:45,628 SendToClientService ExecuteThread: '256' for queue: 'weblogic.kernel.Default (self-tuning)': Received an unknown fault. 16 Jul 2012 11:47:45,650 SendToClientService ExecuteThread: '256' for queue: 'weblogic.kernel.Default... (4 Replies)
Discussion started by: snehasish_jana
4 Replies

7. UNIX for Dummies Questions & Answers

Find last executed timestamp

Hi, on our hp-ux box we have 100s of shell scripts in a bin folder. Now i have to figure out which scripts are not being used at all. The timestamp for these are the ones when they were promoted to this 'bin' folder. Now, how can I find when was the last time each of these scripts were run? I... (2 Replies)
Discussion started by: ysrini
2 Replies

8. Shell Programming and Scripting

Find pattern a delete previous 5 lines

Hi guys, i have the follow problem i need to delete 10 row before the pattern and 1 after and the pattern row itself. file looks like: frect 9.8438 25.8681 10.625 25 . dynprop \ (# \ (call fox_execute(__self))) \ (FOX_VAR_29 \ ... (4 Replies)
Discussion started by: EjjE
4 Replies

9. Shell Programming and Scripting

Script to find previous month last day minus one day timestamp

Hi All, I need to find the previous month last day minus one day, using shell script. Can you guys help me to do this. My Requirment is as below: Input for me will be 2000909(YYYYMM) I need the previous months last day minus 1 day timestamp. That is i need 2000908 months last day minus ... (3 Replies)
Discussion started by: girish.raos
3 Replies

10. Shell Programming and Scripting

How to find the first day of previous month in unix?

How to find the first day of previous month in unix mmddyyyy format? example : today is 07052007 (in mmddyyyy) output sud be 06012007 thanks mohapatra (10 Replies)
Discussion started by: mohapatra
10 Replies
Login or Register to Ask a Question