Greetings, I'm new to this forum, also new to shell script
I have done some simple shell script before, like backup linux machine using rsync and crontab, but now I need to do some log analyzing, which is beyond my ability... so I'm going to seek for help in this forum, hope someone could give me some advice, please.
I would like to ask, how can I process the log line by line? I have to make a Shell Script read the log and do some analyzing line by line, What function can help on this case? (If theres some example of using that function would help me a lot)
Thanks in advance.
awk will process line by line by default.
awk '{print}' <file name> will process line by line giving you the option to add whatever process you need to execute on the all the lines.
Its best that you give us a exemple file with what you are looking to achieve (end result).
Also put whatever you think should be done.
You can use tools like awk, sed, perl....or just with a shell script:
while read line
do
# Do something with $line....
done < logfile
As I always say: There are always more than one way to do it!
You can use:
# Awk
awk '{print}' "<input file>" # As maverick72 commented
# Shell
while read line
do
echo "${line}"
done < "<input file>"
The analysis is with you! =o)
One of my project is to do traceroute log analyzing, if there is a result more than 200ms then the script has to output the current traceroute to another log file.
the traceroute log look like this:
HOST: ADMIN02 Loss% Snt Last Avg Best Wrst StDev
1. 10.x.x.x 0.0% 3 0.1 0.2 0.1 0.2 0.1
2. 118.x.x.x 0.0% 3 2.2 2.0 1.9 2.2 0.2
3. 218.x.x.x 0.0% 3 1.3 1.3 1.3 1.4 0.1
4. 203.x.x.x 0.0% 3 2.6 2.5 2.4 2.6 0.1
I have tried using sed and awk to cut out those useless information and output to a log file only contains the ping, like this:
0.1 0.2 0.1 0.2 0.1
2.2 2.0 1.9 2.2 0.2
1.3 1.3 1.3 1.4 0.1
2.6 2.5 2.4 2.6 0.1
but I stopped here and dont know how to check each value one by one.
Hope these information I provide would help.
Thanks.
awk 'NR > 1 { for ( i=5; i <= NF; i++ ) if ( $i > 200 ) print $0 }' tracert.tmp
2. 118.x.x.x 0.0% 3 2.2 2.0 201.9 2.2 0.2
4. 203.x.x.x 0.0% 3 204.6 2.5 2.4 2.6 0.1
cat tracert.tmp
HOST: ADMIN02 Loss% Snt Last Avg Best Wrst StDev
1. 10.x.x.x 0.0% 3 0.1 0.2 0.1 0.2 0.1
2. 118.x.x.x 0.0% 3 2.2 2.0 201.9 2.2 0.2
3. 218.x.x.x 0.0% 3 1.3 1.3 1.3 1.4 0.1
4. 203.x.x.x 0.0% 3 204.6 2.5 2.4 2.6 0.1
Modified two values from your exemple so mine would work