Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
I have very large log file, which contains log of service restart messages. After I initiated service restart with external command I need to tail this log file from last occurrence of reboot message and check following messages to confirm correct restart procedure. I'm analysing messages by python, so only find last occurrence and follow file needed, then i check output line-by-line and simply close connection when read everything I need.
.... # lots of previous data
[timestamp] previous message
[timestamp] Rebooting... # from tis point i need to track messages
[timestamp] doing thing
[timestamp] doing other thing
[timestamp] doing final thing # final point, reboot successful
[timestamp] service activity message #
How can I perform such tailing?
tail -f <from last Rebooting... message>
give a generous buffer value, reverse, extract, reverse
$ tail -1000 file | tac | awk '1,/Rebooting/' | tac
or, replace awk script with !p; /Rebooting/{p=1}
Perhaps something like:
tail -fn +$(awk '/Rebooting/ { line = NR } END { print(line) }' log) log
which uses awk to find the line number of the last occurrence of the pattern and then tails starting at that line.
This still scans the entire file, though.
If you're really doing it from python, you can probably do better by searching the file in reverse directly in python.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
Improve this question
I want to write a Bash script to look for a new file (recently added file) in a folder and output the name of the new file (including the file type, i.e. example.png) to a variable.
How would I do it?
find . -mmin -10
This command recursively finds all the files and directories in the current folder that have been modified/created in the past 10 minutes.
-mmin stands for minutes. You can also use -mtime which counts days. In addition you can use a + in front of the given number which makes find search for files that have been created/modified at least the given amount of time ago.
If you intend to further parse and use the output, which I assume you do because you intend to store it in a variable, you shouldn't use ls as it optimized for presentation and can change its output format depending on where you write to.
Probably you want to use the -t option of ls:
ls -t
From man ls:
--sort=WORD
sort by WORD instead of name: none (-U), size (-S), time
(-t), version (-v), extension (-X)
--time=WORD
change the default of using modification times; access
time (-u): atime, access, use; change time (-c): ctime,
status; birth time: birth, creation;
with -l, WORD determines which time to show; with
--sort=time, sort by WORD (newest first)
--time-style=TIME_STYLE
time/date format with -l; see TIME_STYLE below
-t sort by time, newest first; see --time
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
Is there any way to filter out absolutely useless messages in bash session by default?
For example, I would like to never see this absolutely useless message: Binary file ... matches while running grep .... It's extremely hard to type something like grep ... 2>/dev/null each time, especially considering how often I need to run this command. Besides it will filter out useful messages as well and this is unwanted.
What I would like to see, is some sort of file in /etc where I could put a bunch of regular expressions of the useless messages line by line. This filter must apply to tty only, i.e. redirected output must stay untouched!
There are some ways to play with your stderr, but there are a number of issues that make that undesirable. For example:
exec 2>/tmp/errorfile
will put all the STDERR output in the errorfile. You could start a
tail -f /tmp/errorfile | grep -v 'Binary file' &
in your .bashrc to get the other messages as well. You will see some funny side effects; for example I found that the prompt is written on STDERR.
You will probably have to create a more elaborate command than the tail|grep to filter-out the undesirable messages and do something about your prompt as well. And you might need to clean-up your errorfile as well.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 years ago.
Improve this question
I'm running a script on terminal and it is supposed to produce a long output, but for some reason the terminal is just showing me the end of the result and I cannot scroll up to see the complete result. Is there a way to save all the terminal instructions and results until I type clear.
The script I'm using has a loop so I need to add the output of the loop if Ill be redirecting the output to a file.
Depending on your system, the size of the terminal buffer may be fixed and hence you may not be able to scroll far enough to see the full output.
A good alternative would be to output your program/script to a text file using:
user#terminal # ./nameofprogram > text_file.txt
Otherwise you will have to find a way to increase the number of lines. In some terminal applications you can go to edit>profiles>edit>scrolling tab and adjust your settings.
You can either redirect the output of your script in a file:
script > file
(Be careful to choose a file that does not exist otherwise the content will be erased)
Or you can buffer the output with less:
script | less
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
I'm using SSH Shell to work with a Unix system and I have a really long query in the command that I would like to edit.
However the part I need to edit is way at the beginning and the only way for me to get there is to hold down the left arrow button for about a minute (each time I need to make a new edit).
Is there a faster way to navigate?
If you are using a ahell with emacs mode command line editing commands you can use the following movement commands (Here a complete reference):
Ctrl-a Move to the start of the line.
Ctrl-e Move to the end of the line.
Alt-f Move forward a word, where a word is composed of letters and digits.
Alt-b Move backward a word.
You may be a bit faster with these.
To get emacs mode with the different shells:
bash: you have to do nothing
ksh: invoke with ksh -o emacs
tcsh: call bindkey -e
Edit: to find out the shell currently in use:
ps -p $$
Example output:
PID TTY TIME CMD
3701 pts/1 00:00:00 ksh
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Improve this question
i want to monitor application log file for specific error patterns on content added in since last 10 min (or since script last run) please not i dont want to monitor entire log file but only lines that are added in last 10 min, when patern is matched i want it displayed on screen. I'm confused how to achieve this thru script.
TIA
regards
tnt5273
FILE=logfile
lines=$(wc -l < "$FILE")
while sleep 600; do
clines=$(wc -l < "$FILE")
diff=$(($clines - $lines))
tail -$diff $FILE | grep PATTERN
lines=$clines
done
What you appear to be describing is commonly achieved at a console by:
tail -F /path/to/my/file | grep "pattern"
This an idiom used by many system adminstrators.
There's another approach where you want to be alerted if a particular event is logged, but you don't want to watch for it.
The Simple Event Correllator is a perl script designed to watch logs, correlate events and perform actions.