To print lines between 2 timestamps using awk|sed and regex


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting To print lines between 2 timestamps using awk|sed and regex
# 8  
Old 04-07-2013
Try with this modification:
Code:
dt_1=$( date +"%h %d, %Y[ ]*%l:[0-9][0-9]:[0-9][0-9] %p" -d "1 hour ago" )
dt_2=$( date +"%h %d, %Y[ ]*%l:[0-9][0-9]:[0-9][0-9] $p" )
awk -v DT1="${dt_1}" -v DT2="${dt_2}" ' {
        n = match ( $0, DT1 )
        if ( n )
                flag = 1
        n = match ( $0, DT2 )
        if ( n )
                flag = 0
} flag ' logfile

This User Gave Thanks to Yoda For This Post:
# 9  
Old 04-08-2013
Code:
dt_1=`date +%h" "%d", "%Y -d  "1 hour ago"`  #Apr 05, 2013 1   --- if ran at 02 AM
dt_2=`date +%l -d  "1 hour ago"`
dt_3=`date +%p -d  "1 hour ago"`  # AM|PM
echo $dt_1
echo $dt_2
echo $dt_3
dt_4=`date +%h" "%d", "%Y`
dt_5=`date +%l 
dt_6=`date +%p`
echo $dt_4
echo $dt_5
echo $dt_6
awk  -v a="{$dt_1}" -v b="{$dt_2}" -v c="{$dt_3}" -v d="{$dt_4}" -v e="{$dt_5}" -v f="{$dt_6}"  '$0~/a int(b):[0-9][0-9]:[0-9][0-9] c/{flag=1;next} $0~/d int(e):[0-9][0-9]:[0-9][0-9] f/{flag=0} flag' /some/logfile >> /some/dump_logs.txt

# 10  
Old 04-09-2013
Thank you Yoda & pravin27

Yoda,

Your guidance helped....The change I made to the scriptlet was that
I omitted '$' sign from dt_1 and d2_2 calculation...and it worked.
Perhaps if you could tell me why you had assigned these variables inside a $ sign.?

Also,I wanted to know...what is the most efficient way to get my script (that I am working on ..) executed on multiple remote servers, one by one and get the result returned (in the form of a file) to my local server ?
something like this on my local server:
for server in `cat $serverlist.txt`
do
ssh user@$server < ./sarah.sh #sarah.sh is a local script
done >> result-on-local-server.txt
# 11  
Old 04-09-2013
Quote:
Originally Posted by sarah-alikhan31
I omitted '$' sign from dt_1 and d2_2 calculation...and it worked. Perhaps if you could tell me why you had assigned these variables inside a $ sign.?
They are command substitution $( ... ) and is preferred over backticks ` ... `
Quote:
Originally Posted by sarah-alikhan31
Also,I wanted to know...what is the most efficient way to get my script (that I am working on ..) executed on multiple remote servers, one by one and get the result returned (in the form of a file) to my local server ?
something like this on my local server:
for server in `cat $serverlist.txt`
do
ssh user@$server < ./sarah.sh #sarah.sh is a local script
done >> result-on-local-server.txt
That is dangerous backticks, I would recommend to use a while loop instead. Also use -n option to get ssh work inside a while loop:
Code:
while read server
do
    ssh -n ${user}@${server} "/path_to_your_script/sarah.sh"
done < serverlist.txt > result-on-local-server.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sed/awk to delete a regex between range of lines

Hi Guys I am looking for a solution to one problem to remove parentheses in a range of lines. Input file module bist_logic_inst(a, ab , dhd, dhdh , djdj, hdh, djjd, jdj, dhd, dhp, dk ); input a; input ab; input dhd; input djdj; input dhd; output hdh; output djjd; output jdj;... (5 Replies)
Discussion started by: kshitij
5 Replies

2. Shell Programming and Scripting

(n)awk: print regex search output lines in one line

Hello. I have been looking high and low for the solution for this. I seems there should be a simple answer, but alas. I have a big xml file, and I need to extract certain information from specific items. The information I need can be found between a specific set of tags. let's call them... (2 Replies)
Discussion started by: Tobias-Reiper
2 Replies

3. UNIX for Advanced & Expert Users

sed REGEX to print multiple occurrences of a pattern from a line

I have a line that I need to parse through and extract a pattern that occurs multiple times in it. Example line: getInfoCall: info received please proceed, getInfoCall: info received please proceed, getInfoCall: info received please proceed, getInfoCall: info received please proceed,... (4 Replies)
Discussion started by: Vidhyaprakash
4 Replies

4. UNIX for Advanced & Expert Users

awk print all fields except matching regex

grep -v will exclude matching lines, but I want something that will print all lines but exclude a matching field. The pattern that I want excluded is '/mnt/svn' If there is a better solution than awk I am happy to hear about it, but I would like to see this done in awk as well. I know I can... (11 Replies)
Discussion started by: glev2005
11 Replies

5. Shell Programming and Scripting

Print lines that match regex on xth string

Hello, I need an awk command to print only the lines that match regex on xth field from file. For example if I use this command awk -F"|" ' $22 == "20130117090000.*" 'It wont work, I think, because single quotes wont allow the usage of the metacharacter star * . On the other hand I dont know... (2 Replies)
Discussion started by: black_fender
2 Replies

6. Shell Programming and Scripting

Print lines between two strings multiple occurencies (with sed, awk, or grep)

Hello, I can extract lines in a file, between two strings but only one time. If there are multiple occurencies, my command show only one block. Example, monfichier.txt contains : debut_sect texte L1 texte L2 texte L3 texte L4 fin_sect donnees inutiles 1 donnees inutiles 2 ... (8 Replies)
Discussion started by: theclem35
8 Replies

7. Shell Programming and Scripting

How to print the lines between the pattern using awk/grep/sed?

Hi, I need a help to search a pattern and print the multiple lines between them. Input file: Tue May 29 12:30:33 EDT 2012:threadWebContainer : 357:com.travimp.hotelierlinks.abba.service.RequestHandler.requestService(String, ITICSDataSet): hotelCancelReservation request: ... (4 Replies)
Discussion started by: aroragaurav.84
4 Replies

8. Shell Programming and Scripting

Print lines after regex

Hello, I need to print four lines inmediatly after the regexp, but not the line containing the regexp. The print should show the four lines together in one. Thanks! (13 Replies)
Discussion started by: auratus42
13 Replies

9. Shell Programming and Scripting

print first few lines, then apply regex on a specific column to print results.

abc.dat tty cpu tin tout us sy wt id 0 0 7 3 19 71 extended device statistics r/s w/s kr/s kw/s wait actv wsvc_t asvc_t %w %b device 0.0 133.2 0.0 682.9 0.0 1.0 0.0 7.2 0 79 c1t0d0 0.2 180.4 0.1 5471.2 3.0 2.8 16.4 15.6 15 52 aaaaaa1-xx I want to skip first 5 line... (4 Replies)
Discussion started by: kchinnam
4 Replies

10. Shell Programming and Scripting

sed - print only matching regex

Hi folks, Lets say I have the following text file: name, lastname, 1234, name.lastname@test.com name1, lastname1, name2.lastname2@test.com, 2345 name, 3456, lastname, name3.lastname3@test.com 4567, name, lastname, name4.lastname4@test.com I now need the following output: 1234... (5 Replies)
Discussion started by: domi55
5 Replies
Login or Register to Ask a Question