Grep for last X hours [duplicate] - linux

This question already has answers here:
Extract data from log file in specified range of time [duplicate]
(5 answers)
Closed 7 years ago.
I have a log file. Each line is prepended with a date and the date format is as below:
2016-02-01 11:34:48,567.......
How do I grep this file for last 24 hours?
I tried few things such as below for specific time but this approach wont work for last X hours:
grep "2016-02-01 15:0[1-9]:00 logfile
Also, the following does work for -1hour but fails for -23 or -24hours:
grep "^$(date -d -23hour +'%Y-%m-%d %H')" logfile

You can use sed if lines are arranged in chronological order.
sed -e "1,/^$(date -d -23hour +'%Y-%m-%d %H')/d"
This will delete all lines until the first match is found ie print all line after the first match till the end of file.

Related

How to remove date from filename linux [duplicate]

This question already has answers here:
Rename multiple files while keeping the same extension on Linux
(4 answers)
Closed 3 years ago.
I have a scenario where I want to remove date from filename
Lets take an example 1 :
ABC_2019_06_12.txt
Lets take an example 2 :
ABCDEF_202012040120456.txt
using cut I cannot delete required text
how to cut to get the required below output like below
ABC.txt
ABCDEF.txt
One command which should work for all scenario which ever filename it is
My solution which I worked is to read the number of position and cut that part but I don't find it effective any other solution will be appreciated
In bash you can cut off the part starting with underscore:
$ filename=ABC_2019_06_12.txt
$ filename=${filename%%_*}
$ echo $filename
ABC

How to Save 'specific' line from terminal output to file? [duplicate]

This question already has answers here:
Bash tool to get nth line from a file
(22 answers)
Closed 4 years ago.
I am currently using the following to save terminal outputs to file:
$command -someoptions >> output.txt
However, I am only interested in one line from the terminal output.
Is there a way to do this by changing the above expression. Or will I have to delete lines after the 'output.txt' file is formed?
For example: If my output is:
line 1
line 2
line 3
line 4
line 5
and all I want to save is:
line 4
where line 4 contains unknown information.
I am asking as I will later wish to script this command.
Many thanks,
Solution Found:
I ended up using:
$command -someoptions | sed -n '4p' >> output.txt
This is a classic simple grep issue.
$command -someoptions | grep 'line 4' >> output.txt
You could refine that with more pattern complexity, and might need it depending on how precisely you need to match the data.
Try with this command:
$command -someoptions | grep " filter " >> output.txt
filter must be replaced by an element that distinguishes your line 4 from the other lines.

How do I add time to a date in unix shell script? [duplicate]

This question already has answers here:
Date arithmetic in Unix shell scripts
(14 answers)
How do I do date math in a bash script on OS X Leopard?
(8 answers)
Calculating time (adding minutes) bash
(1 answer)
Subtract days from a date in Bash
(7 answers)
Closed 4 years ago.
I have a file that contains date values. I want to be able to pull the last line of the file, formatted like "'2018-09-18 16:42:57'" add 1 day to it and store that into a variable. The code I have right now looks like the following, but it does not work:
start_date=$(tail -n 1 run_dates.txt)
start_date=$(start_date -d "+1 day")
What is the correct syntax to do this?
You can use this one-liner gnu date command to extract last line of the file, add one day and store output in a variable:
start_date=$(date -d "$(tail -n 1 run_dates.txt) +1 day" '+%Y-%m-%d %T')
To check variable content use:
declare -p start_date
declare -- s="2018-09-19 11:42:57"

finding specific pattern in linux [duplicate]

This question already has answers here:
Print only matching word, not entire line through grep
(2 answers)
Closed 5 years ago.
I want to find specific pattern in all the files in a directory and copy them to another line
For E.g
I want to find LOG_WARNING in one file XYZ and copy them to another file.
LOG_WARNING (abc, xyz,("WARNING: Error in sending concurrent_ to pdm\n"));
command i have used is :
grep -rin "LOG_WARNING.*" file_name.c > output.txt
but it is not copying till the semicolon, please note that other texts are available in next line. I want to copy till ;(semi-colon)
grep -rh "LOG_WARNING" * > out.txt
This will match the pattern in all the files inside the directory.
Since you mentioned that the texts that are present after the ';' are on the next line, I have provided this command.
This will match the pattern and print the entire line, till the ';'.
Else,
try this
grep -roPh 'LOG_WARNING[^;]*;' * > out.txt

How to tail all lines except first row [duplicate]

This question already has answers here:
how to read file from line x to the end of a file in bash
(7 answers)
Closed 7 years ago.
For example, I have a file
1
2
3
then I want to output from 2nd row to tail
How can I do it in linux
tail -n+2 my_file
will output all the lines in myfile starting with line 2. (-n2 would show you the last two lines.)
tail has lots more options. Type man tail for complete documentation.
shorter with
$ sed 1d filename
or with awk
$ awk 'NR>1' filename

Resources