Script to process file from a directory and grep the required content and print


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to process file from a directory and grep the required content and print
# 8  
Old 02-08-2013
Sorry RudiC. I didn't quite get you.
Please correct me if i understood wrong here, i created one more file by name header and
it contains all the echo statments to print header. Then i called the main script as ./output.sh header
and in main script this line remains same :
Code:
' FS=":"  header ????_Daily_Check.log

output getting now is :
Code:
root@bbt # ./output.sh header
awk: syntax error near line 13
awk: bailing out near line 13

# 9  
Old 02-08-2013
No echo statements! header should contain in plain text what you would like to see in the first lines of your output.
And a shell script like output.sh is not mandatory - run the awk program from the command line. If satisfied, you can put it into any script you want, of course.
# 10  
Old 02-08-2013
Thanks RudiC.

I have question, can't put this header info in the same awk command. Since i want to make this awk command as function and calling them in my main script.

Then for some of the Passed string color didn't appear as green it still shows in white and how i get the status string(passed/failed) alligned properly

Code:
root@bbt # ./output.sh
Server              Interface Checks                  Process Checks          Service Check
              VerifyInterface  Corefile     VerifyRegister VerifyComponent
-------      ----------------  ---------     -------------  -------------     -------------
D01A                   Passed     Failed                        Passed          Passed      Passed

# 11  
Old 02-08-2013
Then, remove this line
Code:
     NR == FNR     {print; next}                  # read & print header

and the header file name in the parameter list.
As the colour control strings are sensitive to the exact spelling of the Passed / Failed words, I guess your input file has sth. different in it (<TAB> char maybe?). Post it, please.
# 12  
Old 02-08-2013
Input file look like this :
Code:
root@bbt #  cat D01B_Daily_Check.log
 
1.Interface checking
 
Passed:VerifyInterface:D01B : All interface cards are up
 
Failed:CoreFileGenerated:D01B : There is no core dump file.
 
2.Process checking
 
        Passed:VerifyRegister:D01B : All products have been REGISTERED
 
        Passed:VerifyComponent:D01B : Some Component Not RUNNING.  dbt 0/1 :
 
3.Checking Service available
 
Passed:VerifyServiceAvailability:D01B: Service PERM_DRV is NOT up.

If am not wrong, guess from the input file point 2 status string are placed after few spaces and in the script not sure where to include the logic to remove the preceeding blanks.

point 2 from input file :
Code:
2.Process checking

        Passed:VerifyRegister:D01B : All products have been REGISTERED

        Passed:VerifyComponent:D01B : Some Component Not RUNNING.  dbt 0/1 :

# 13  
Old 02-08-2013
Sure that it's spaces and not <TAB>? I'm removing all spaces with the gsub(...) function. Modify from gsub(" ","",$1) to gsub(/[ \t]/,"",$1).
# 14  
Old 02-08-2013
no need for calling color codes in every run/print statement, try setting the vars as and then use them according

awk -v B=`tput smso` -v N=`tput rmso` '{$1= B $1 N} {$2= B $2 N} {$3= B $3 N} {print}'



Quote:
Originally Posted by Optimus81
Hi Bipinajith,

I did tried you solution but still not able to get the colors on the string Passed/Failed.

It still shows the string in white color, that means assigning variable s is not working.
I did tried something with printf stmt even that didn't work.

Code:
 for file in *_Daily_Check*
do
        server=${file%%_*}
        if [ -f "$file" ]
        then
                nawk -F":" -v S="$server" '/VerifyInterface/ {
                       #printf "\033[1;36m|%s\033[0m%20s", S, $1;if (s == "Passed"){$1 = "\033[1;32m" s "\033[0m"
                       if($1=="Passed") s="\033[1;32m $1 \033[0m";
                       if($1=="Failed") s="\033[1;31m $1 \033[0m";
        
        #if($1=="Passed") printf "\033[1;32m|%s\033[0m%20s",s,$1;
                       #if($1=="Failed") printf "\033[1;31m|%s\033[0m%20s",s,$1;
        
                       printf "\033[1;36m|%s\033[0m%20s", S, $1;
                } /CoreFileGenerated/ {
                        #printf "\033[1;32m%15s\033[0m", $1;
                        if(s=="Passed"){ $1="\033[1;32m" s "\033[0m";}
                        if(s=="Failed"){ $1="\033[1;31m" s  "\033[0m";}
                        printf "%15s",$1;
      
                       #if($1=="Passed") printf "\033[1;32m|%s\033[0m%20s",s,$1;
                       #if($1=="Failed") printf "\033[1;31m|%s\033[0m%20s",s,$1;
        
                } /VerifyRegister/ {
                        #printf "\033[1;32m%15s\033[0m", $1;
                        if($1=="Passed") s="\033[1;32m" $1 "\033[0m";
                        if($1=="Failed") s="\033[1;31m" $1 "\033[0m";
                        printf "%15s", $1;
                } /VerifyComponent/ {
                        #printf "\033[1;32m%15s\033[0m", $1;
                        if($1=="Passed") s="\033[1;32m" $1 "\033[0m";
                        if($1=="Failed") s="\033[1;31m" $1 "\033[0m";
                        printf "%15s", $1;
                } /VerifyServiceAvailability/ {
                        #printf "\033[1;32m%15s\033[0m     |\n", $1;
                        if($1=="Passed") s="\033[1;32m" $1 "\033[0m";
                        if($1=="Failed") s="\033[1;31m" $1 "\033[0m";
                        printf "%15s     |\n", $1;
                } ' "$file"
           echo "|--------------------------------------------------------------------------------------------------------|"
        fi
done

@RudiC,
Thanks for your solution. It more like making script generic, which is very good. but when i tried to execute
you script, i get the below error which i was not able to resolve :
Error:
Code:
root@bbt # ./output.sh
nawk: can't open file header
 source line number 11

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Directory containing files,Print names of the files in the directory that are exactly same content.

Given a directory containing say a few thousand files, please output a list of all the names of the files in the directory that are exactly the same, i.e. have the same contents. func(a_directory_name) output -> {“matches”: , ... ]} e.g. func(“/home/my/files”) where the directory... (7 Replies)
Discussion started by: anuragpgtgerman
7 Replies

2. Shell Programming and Scripting

Script to search every file in a directory and print last few lines

Hi everyone, I need to write a script to search a directory, output the name of a file to an ouput file and print the last few lines of the files to the output file such that I would have something like this: FILE1: LINE LINE LINE FILE2: LINE LINE LINE FILE3: LINE LINE LINE... (2 Replies)
Discussion started by: mojoman
2 Replies

3. Shell Programming and Scripting

Help required to Print Single quote into a file

Hi, I need help in printing string enclosed with single quotes to a file. I am trying to write a shell script which when run will create another script below is the script logic. cat create_script.sh echo '#!/bin/sh' > append_flname.sh echo 'for FILE in $*' >> append_flname.sh echo... (6 Replies)
Discussion started by: imrandec85
6 Replies

4. Shell Programming and Scripting

How to Modify a file content in UNIX and sort for only required fields ?

I have the below contents in a file after making the below curl call curl ... | grep -E "state|Rno" | paste -sd',\n' | grep "Disconnected" > test "state" : "Disconnected",, "Rno" : "5554f1d2" "state" : "Disconnected",, "Rno" : "10587563" "state" : "Disconnected",, "Rno" :... (2 Replies)
Discussion started by: Vaibhav H
2 Replies

5. Shell Programming and Scripting

Bash script to read a file from particular line till required line and process

Hi All, Am trying to write wrapper shell/bash script on a utility tool for which i need to pass 2 files as arugment to execute utility tool. Wraper script am trying is to do with above metion 2 files. utility tool accepts : a. userinfo file : which contains username b. item file : which... (2 Replies)
Discussion started by: Optimus81
2 Replies

6. Shell Programming and Scripting

Shell script to monitor new file in a directory and mail the file content

Hi I am looking for a help in designing a bash script on linux which can do below:- 1) Look in a specific directory for any new files 2) Mail the content of the new file Appreciate any help Regards Neha (5 Replies)
Discussion started by: neha0785
5 Replies

7. Shell Programming and Scripting

Goto each directory and subdirectories and grep the required pattern

Hi All, I need your help in finding pattern from files present in different directories. I need to search for a pattern "xyz" from "*.txt" files which are present in different levels of directories as shown. example ------- dir1/subdir1/file.txt dir2/subdir2/subsubdir2/file.txt... (5 Replies)
Discussion started by: imas
5 Replies

8. UNIX for Dummies Questions & Answers

Print the content of a directory in jpg file

Is there any possible way to print the contents of a directory to a .jpg file? I have a list of thumbnails (e-books) which I want to share (+500) but I don't know how to make this. I would appreciate a lot any comments regarding this issue. (4 Replies)
Discussion started by: agasamapetilon
4 Replies

9. Shell Programming and Scripting

shell script to search content of file with timestamps in the directory

hello, i want to make a script to search the file contents in my home directory by a given date and output me the line that has the date... (10 Replies)
Discussion started by: psychobeauty
10 Replies
Login or Register to Ask a Question