Grep from match to the end of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep from match to the end of file
# 1  
Old 12-05-2016
Grep from match to the end of file

Hi

i have this script :

Code:
#!/bin/bash
DATE=$(date '+%Y-%m-%d %H:%M' -d  "1 hour ago")

PASS=$(grep -A99999999999  '$DATE'  /var/log/asterisk/full| grep -i 'Wrong password')

it a script that ment to go over log file a hour back from now until
the end of file.

right now im using -A999 - do you have more prudactive way ?
when i searched all lead to use awk but as im looking for 1 hour backwords its becoming complicated.

ideas ?

Last edited by Scrutinizer; 12-05-2016 at 11:59 AM.. Reason: icode tags => code tags
# 2  
Old 12-05-2016
If you can get the line number from the file in some way, you can display the file from that point on with (amongst other things) a sed

If you were to do something like this, would it help?:-
Code:
last_line=$(grep -n "Your search text here" input_file | tail -1 | cut -f 1 -d ":")

((last_line=$last_line-1))               # To include current line in the next step

sed -n "$last_line,\$"p input_file



I hope that this is useful,
Robin
# 3  
Old 12-05-2016
Any string search to match $DATE will work only if the exact minute is found in the log file. E.g.
Code:
sed -n "/$DATE/,$ p" file

If that fails, you'd need e.g. awk to do a "larger than" comparison. Try (untested)
Code:
awk -v DT="$DATE" '($1 " " $2) >= DT' file

- given the time stamp is in $1 and $2, separated by a space.
# 4  
Old 12-06-2016
thanks for your answer!

rbatte1:

Code:
last_line=$(grep -n "Your search text here" input_file | tail -1 | cut -f 1 -d ":")

((last_line=$last_line-1))               # To include current line in the next step

sed -n "$last_line,\$"p input_file

what it did is to give me the line in the log which it have found my word:
Code:
last_line=$(grep -n "Wrong password" /var/log/asterisk/full-20161205 | tail -1 | cut -f 1 -d ":")

gave me : 37171

then i did
Code:
((last_line=$last_line-1))

whichgave me this :
37171-1 as a varible

and when i ran
Code:
sed -n "$last_line,\$"p  /var/log/asterisk/full-20161205

i got :
Code:
sed: -e expression #1, char 6: unknown command: `-'

------------------------------------------------------------------------------

RudiC:

thanks ! it did the trick :

Code:
awk -v DT="$DATE" '($1 " " $2) >= DT'   /var/log/asterisk/full-20161205 | grep "password"

finds from hour back to the end of file! Smilie
Moderator's Comments:
Mod Comment Please use CODE tags (not ICODE tags) when displaying full line and multi-line sample input, sample output, and code segments.

Last edited by Don Cragun; 12-06-2016 at 02:54 AM.. Reason: Change most ICODE tags to CODE tags.
# 5  
Old 12-06-2016
Why use grep? Try
Code:
awk -v DT="$DATE" '/password/ && ($1 " " $2) >= DT' /var/log/asterisk/full-20161205

# 6  
Old 12-06-2016
Quote:
Originally Posted by RudiC
Why use grep? Try
Code:
awk -v DT="$DATE" '/password/ && ($1 " " $2) >= DT' /var/log/asterisk/full-20161205

thanks!
another thing that was missing for me was "[" befor the DATE variable

Code:
awk -v DT="[$DATE" '/password/ && ($1 " " $2) >= DT' /var/log/asterisk/full-20161205


THANKS

Last edited by rbatte1; 12-06-2016 at 06:24 AM.. Reason: Changed ICODE tags to CODE tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep a log file starting from a specific time to the end of file

I have a log file which have a date and time at the start of every line. I need to search the log file starting from a specific time to the end of file. For example: Starting point: July 29 2018 21:00:00 End point : end of file My concern is what if the pattern of `July 29 2018 21:00:00`... (3 Replies)
Discussion started by: erin00
3 Replies

2. Shell Programming and Scripting

Grep for an exact match in a file

I am currently having some issues while trying to grep for a exact string inside a file. I have tried doing this from command line and things work fine i.e. when no match is found, return code=1 but when its done as part of my script it returns 0 for the same command - I dont know if there is an... (6 Replies)
Discussion started by: Ads89
6 Replies

3. Shell Programming and Scripting

Search from 1st match and end 2nd match

I've been looking through the forums for awhile now and looking at the man page for grep and egrep and not seeming to find this scenario so it might not be possible but figured I'd throw it out to get some ideas. I'm looking for a way to search a file for 1st match (example below net self) and... (3 Replies)
Discussion started by: djzah
3 Replies

4. Shell Programming and Scripting

Grep start and end line of each segments in a file

Cat file1 -------- ---------- SCHEMA.TABLE1 insert------- update----- ------------- ---------- SCHEMA.TABLE2 insert------- update----- ----------- ------------ SCHEMA.TABLE3 insert------- update----- ------------ grep -n SCHEMA > header_file2.txt (2 Replies)
Discussion started by: Veera_V
2 Replies

5. Shell Programming and Scripting

Pattern match till the end of the file.

I have a file which is like this ……………………………………….. ………………………………… ………………………………… …………………………………… ……………………………………. ……………………………… <<<from_here>>> ……………………………… ………………………………. I want a script which would fetch the data starting from <<<from_here>>> in the file till the end... (2 Replies)
Discussion started by: halfafringe
2 Replies

6. Shell Programming and Scripting

AWK-grep from line number to the end of file

Does anyone know how to use awk to act like grep from a particular line number to the end of file? I am using Solaris 10 and I don't have any GNU products installed. Say I want to print all occurrences of red starting at line 3 to the end of file. EXAMPLE FILE: red green red red... (1 Reply)
Discussion started by: thibodc
1 Replies

7. Shell Programming and Scripting

sed print from last occurrence match until the end of file

Hi, i have file f1.txt with data like: CHECK a b CHECK c d CHECK e f JOB_START .... I want to match the last occurrence of 'CHECK' until the end of the file. I can use awk: awk '/^CHECK/ { buf = "" } { buf = buf "\n" $0 } END { print buf }' f1.txt | tail +2Is there a cleaner way of... (2 Replies)
Discussion started by: ysrini
2 Replies

8. Shell Programming and Scripting

Grep from a starting line till the end of the file

Hi Folks, I got to know from this forums on how to grep from a particular line say line 6 awk 'NR==6 {print;exit}' But how do i grep from line 6 till the end of the file or command output. Thanks, (3 Replies)
Discussion started by: Mr. Zer0
3 Replies

9. UNIX for Dummies Questions & Answers

Lynx Grep Pattern Match 2 conditions Print from Start to End

I am working on a scraping project and I am stuck at this tiny grep pattern match. Sample text : FPA List. FPA List. FPA List. FPA List. FPA List. FPA List. FPA List. FPA List. ABC Personal Planning Catherine K. Wat Cath Wat Catherine K. Wat Catherine K. Wat IFRAME:... (8 Replies)
Discussion started by: kkiran
8 Replies

10. UNIX for Dummies Questions & Answers

Grep the word at the end of the file

I need to grep the word "hello" in each and every file. This word will be placed either at the end of the file or before the end of the file. Ex: file1: file2: afdsaf dsfalk fdsa weruoi sdaf erwqiuo fsdaf ... (5 Replies)
Discussion started by: sivakumar.rj
5 Replies
Login or Register to Ask a Question