Find keywords in multiple log files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find keywords in multiple log files
# 1  
Old 05-31-2016
Find keywords in multiple log files

I have several problems with my program: I hope you can help me.

1) the If else statement isn't working . The IF Else syntax is:
If MEMSIZE OR sasfoundation (SASEXE) OR Real Time(second) >1.0 and Filename, output column name and value to csv or else nothing

Example progflag,cvs:
Code:
Memsize                 Second                 SASEXE                                   filename
    400                       4.0                         SASFoundaion                   file11.log.20120314



2) I am not getting any data in the csv file

3) The email syntax isn't working. I am not receiving the cvs file attachment via email


My program read in multiple files with .log, extension. For example file12.log.20120314. The program search for 3 selected items in each log files.

Item 1# : Memsize . Memsize statement stores numeric values. For example memsize=400. the program output the column name (memsize) and its value and the filename to a csv file

example - progflag.csv:
Code:
memsize              filename
 400                       file12.log.20120314

Item 2# : Real Time; row value. For example, the row value for Real Time is 4.0. Real Time : 4.0.
In my program Real Time is named Second. For example, SECOND stores 4.0. IF SECOND > 1.0 then output the column name
and its value to a cvs file

example - progflag.csv:

Code:
Second                 filename
 4.0                      file11.log.20120314

If Real Time  row value is less than 1.0 then output no data to the cvs file.

Example  Real Time: 0.2         0.2 is less than 1.0

item3#: if the program find the directory path /SASFoundation (SASEXE) then output the directory path to a cvs file

Example progflag.cvs
Code:
Second        SASEXE                                 filename
 4.0               SASFoundaion                   file11.log.20120314Here is the code:

Code:
cd /tmp/*.log.*
awk -F '[=:;.]' '
  function pr() {if(NR>1) printf "%s\t%s\t%s\t%s\n", K[1],K[2],K[3],K[0]}
  BEGIN {
      printf "MEMSIZE\tSECOND\tSASEXE\tFilename\n"
      for(i=split("memsize ,Real Time ,SASFoundation",A,",");i;i--) L[A[i]]=i
  }
  FNR==1 {
      pr()
       K[0]=FILENAME
      K[1]=K[2]=K[3]=x
  }
  $1 in L {v=$2;gsub("^[/ ]*","",v);gsub(/ *$/,"",v);K[L[$1]]=v}
  END{pr()}
if MEMSIZE OR SECOND >1.0 OR  SASEXE AND Filename then
' *.log.* > progflag.csv

[ -s progflag.csv ] && mailx -s "subject text -a "Programs flagged" receiver@domain.com < progflag.csv
ELSE ''

Moderator's Comments:
Mod Comment You have been asked many times before to use CODE tags when displaying ALL sample input, ALL sample output, and ALL code segments.

Please follow the forum rules and use CODE and ICODE tags when displaying ALL sample input, output, and code segments.
That does NOT mean that explanatory text should be include in CODE tags. Only sample input, sample output, and code segments!

Last edited by Don Cragun; 06-01-2016 at 08:27 PM.. Reason: Remove CODE tags surrounds explanatory text.
# 2  
Old 05-31-2016
So what are the symptoms of your problem?

Are you getting syntax errors for an incomplete awk program?

Are you getting failures from cd for trying to change directory to a list of four regular files (instead of to one directory)?

Are you always getting mail because progflag.csv is never empty since you always print a header line into that file even if no data follows the header?
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 05-31-2016
I'm giving up.

On top of what Don Cragun said,
- the attached files' names don't match the ones mentioned in the text
- the attached files are no *nix text files as they are lacking the trailing <new line> char
- the attached files' structure (case, spaces around "=", maybe more) doesn't match the one mentioned in the text or inferred from the code sample.

Why don't you take a step back, rephrase the specification and explain the logics needed using input sample data and showing how they should show up in the output?
# 4  
Old 06-01-2016
answer your question :

Are you getting syntax errors for an incomplete awk program?
I am get syntax errors for an incomplete awk program

I am getting an error in the If else statement


Are you always getting mail because progflag.csv is never empty since you always print a header line into that file even if no data follows the header?

I am not receiving email
# 5  
Old 06-01-2016
Maybe you should consider the questions I asked and the comments RudiC made as suggestions for things to change in your code to make it work correctly and avoid the problems you are having. If you try fixing those problems in your code and are still having problems, come back to us and:
  1. Tell us what operating system you're using.
  2. Tell us what shell you're using.
  3. Clearly describe the format of the input and output files you are processing? (Are they UNIX format text files? If not, why not and what format are they?)
  4. Show us sample input files (in CODE tags).
  5. Show us sample output files showing the exact output you are trying to produce from your sample input files (in CODE tags).
  6. A CLEAR specification of what you are trying to do (using filenames in your specification that match the sample input and output files specified above.)
  7. Show us your updated code (in CODE tags).
  8. Show us all of the diagnostic messages that are being produced from your code (in CODE tags). (And, don't tell us that mailx is failing when your script died long before it got to mailx.)
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 06-02-2016
Find keywords in multiple log files

Answer to your questions:
Tell us what operating system you're using? AiX

Tell us what shell you're using? bash shell


The format of text files is based on generated sas programs that produce sas log files. The sas programmers sometimes add the following parameters to their code Memsize, and a directory path /sas/sasfoundation. A programmer doesn't always add Memsize or a directory path /sas/sasfoundation in his code.

Therefore the output in his log file will not have Memsize or a directory path
/sas/sasfoundation.

In all the log files, there is an assignment variable named Real Time with a numeric value. Real Time value is normally low. The value range between 0.0 - 0.9. Real Time value is high if the value is 1.0


I have several problems with the program: I hope you can help me.

1) the If else statement is throwing an error message. syntax error can't
Code:
    read
    {if ($1)|| ($2>1.0) || ($ 3) && ( $0)) printf $1 "\t" $2 "\t"" $3"\t" $0"\t";   
    elseif($2 < 1.0  else print ''}'
    ' *.log > progflag.csv.txt

What the below syntax is saying :
  1. if ($1)
    Memorize = ; , there a numeric value after the = in the log files, then output the value to progflag.csv.txt'
  2. or if ($2>1.0)
    Second which is alias for Real Time : , there is a numeric value after the : in the log files, greater than 1.0 then output the value to progflag.csv.txt'
  3. or if ($ 3)
    sasfoundation is the value that is stored in the alias sasexe.
    if sasfoundation exist in the logfile then output value to progflag.csv.txt,
  4. and ($0)
    filename. Each log file has a title. if if ($1)|| ($2>1.0) || ($ 3) && ( $0))
    then output the each log file record with the filename to progflag.csv.txt
  5. elseif($2 < 1.0 else print ''}
    This means if $2 is less than 1.0 then no value is outputted to the column
    named Second in the progflag.csv.txt


For example in filew.log if the following items don't exist: Memsize, SASFoundation and also if Real Time row value is less than 1.0 then no data is outputted to progflag.cvs.txt

The below show sample of the exact output I'm trying to produce from the sample input files to progflag.cvs.txt. filew.log.txt data isn't in the progflag.cvs.txt because it doesn't have the following criteria Memsize, SASFoundation and Real Time value greater than 1.0 :
Code:
Memsize        Second        SASEXE                  filename.txt
    200                            SASFoundation           file1x.log.txt
    100                            SASFoundation           file2x.log.txt
    400           5.1                                            filez.log.txt

2) I am not getting any data in progflag.cvs.txt even though Memsize, and SASFoundation are in some of the log files that the program reads in


3) I am not receiving the progflag.cvs attachment via email

4) I added *.log | awk because I want the program to read in log files with the .log extension only. There are other files in the directory that have different extensions.

Code:
'*.log | awk -F '[=:;.]' '
  function pr() {if(NR>1) printf "%s\t%s\t%s\t%s\n", K[1],K[2],K[3],K[0]

I am getting the following error
Code:
*.log |awk -F [=:: not found.
   .] not found. syntax error at line 3: '(' not expected


what The program is doing is the followings:

searches for 3 selected items in each log files:
  1. Memsize= ; 'a numeric value is after the ='
  2. sasfoundation - the path in a directory,
  3. Real time : 'a numeric value is after the :'
Code:
#!/bin/bash
cd /tmp/logs

'*.log | awk -F '[=:;.]' '
  function pr() {if(NR>1) printf "%s\t%s\t%s\t%s\n", K[1],K[2],K[3],K[0]}
  BEGIN {
      printf "MEMSIZE\tSECOND\tSASEXE\tFilename\n"
      for(i=split("memsize ,Real Time ,SASFoundation",A,",");i;i--) L[A[i]]=i
  }
  FNR==1 {
      pr()
       K[0]=FILENAME
      K[1]=K[2]=K[3]=x
  }
  $1 in L {v=$2;gsub("^[/ ]*","",v);gsub(/ *$/,"",v);K[L[$1]]=v}
  END{pr(
{if ($1) || ($2>1.0 ) || ( $ 3 ) &&  ($0)) printf $1 "\t" $2 "\t" $3"\t" $0"\t; elseif($2 < 1.0 else print ''}'
' *.log > progflag.csv

[ -s progflag.csv ] && mailx -s "subject text -a "Programs flagged" receiver@domain.com < progflag.csv

Moderator's Comments:
Mod Comment I have made a best effort attempt to correctly format your post. But, I am not able to understand a lot of what you are saying. I repeat:
  1. Use CODE tags to display sample intput.
  2. Use CODE tags to display sample output.
  3. Use CODE tags to display code.
  4. Do NOT use CODE tags to display questions, answers, or explanatory text.
  5. Use QUOTE tags to display text that is copied from another post or an external source. Do not use QUOTE tags to display explanatory text.

Last edited by Don Cragun; 06-03-2016 at 07:00 PM.. Reason: Remove CODE tags surrounding explanatory text, add ICODE tags, add LIST tags, anremove QUOTE tags around seeming plain text.
# 7  
Old 06-03-2016
We are lost here.
You have uploaded four sample files: file1.log.02896.txt, file2.log.02897.txt, filew.log.02820.txt, and filez.log.02899.txt
None of these files are referenced in any of your posts in this thread.

You have referenced files with names (or names that match pathname matching patterns) *.log, progflag.cvs, progflag.cvs.txt, and several others; but you have not shown us samples of the contents of any of these files.

You have shown us some code and you have sort of said what some of that code is trying to do, but the syntax is so different from the syntax expected by awk and bash and your explanations are not in complete sentences, so I am unable to figure out the format of your input files and I am unable to figure out the logic you are trying to use to produce the output you want.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find and replace from multiple files

Hello everybody, I need your help. I have a php site that was expoited, the hacker has injected into many php files a phishing code that was discovered and removed in order to have again a clean code. Now we need to remove from many php files that malware. I need to create a script that find and... (2 Replies)
Discussion started by: ninocap
2 Replies

2. Shell Programming and Scripting

Grep multiple keywords from a file

I have a script that will search for a keyword in all the log files. It work just fine. LOG_FILES={ "/Sandbox/logs/*" } for file in ${LOG_FILES}; do grep $1 $file done This only works for 1 keyword. What if I want to search for more then 1 keywords, say 4 or maybe even... (10 Replies)
Discussion started by: Loc
10 Replies

3. UNIX for Dummies Questions & Answers

Find keywords in multiple log files

The Problem that I am having is when the code ran and populated the progflag.csv file, columns MEMSIZE, SECOND and SASEXE were blank. The next problems are the IF else statement isn't working and the email function isn't sending the progflag.csv attachment. a. What I want the program to do is to... (2 Replies)
Discussion started by: dellanicholson
2 Replies

4. Shell Programming and Scripting

Search files in directory for keywords using bash

I have ~100 text files in a directory that I am trying to parse and output to a new file. I am looking for the words chr,start,stop,ref,alt in each of the files. Those fields should appear somewhere in those files. The first two fields of each new set of rows is also printed. Since this is on a... (7 Replies)
Discussion started by: cmccabe
7 Replies

5. Shell Programming and Scripting

Find keywords, and append at the end of line

Task: Find keywords in each line, and append at the end of line; if not found in the line, do nothing. the code is wrong. how to make it work. thanks a lot. cat keywords.txt | while read line; do awk -F"|" '{if (/$line/) {print $0"$line , ";} else print;}' outfile.txt > tmp ... (9 Replies)
Discussion started by: dtdt
9 Replies

6. UNIX for Advanced & Expert Users

Need to search for keywords within files modified at a certain time

I have a huge list of files in an Unix directory (around 10000 files). I need to be able to search for a certain keyword only within files that are modified between certain date and time, say for e.g 2012-08-20 12:30 to 2012-08-20 12:40 Can someone let me know what would be the fastest way... (10 Replies)
Discussion started by: virtual123
10 Replies

7. UNIX for Dummies Questions & Answers

finding keywords in many files using grep

Hi to all Sorry for the confusion because I did not explain the task clearly. There are many .hhr files in a folder There are so many lines in these .hhr files but I want only the following 2 lines to be transferred to the output file. The keyword No 1 and all the words in the next line They... (5 Replies)
Discussion started by: raghulrajan
5 Replies

8. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

9. Shell Programming and Scripting

Finding 50k Keywords in 3k files

Hi, I have a file with about 50k keywords. I have a requirement to scan about 3k files to identify which filename has which keyword i.e. an output like following: File1,Keyword1 File1,Keyword2 File3,Keyword1 ..... I have written a shell script which takes each of the 3k files, searches... (4 Replies)
Discussion started by: rjains
4 Replies

10. Shell Programming and Scripting

How to find particular string in multiple files

Hello friends, I have find a paticular string from the files present in my user for example: a username and password is hardcoded in multiple files which present in the my user.so I have to search about username in which files it is available.there are several dirctories are there,so... (5 Replies)
Discussion started by: sivaranga001
5 Replies
Login or Register to Ask a Question