Script to process file from a directory and grep the required content and print
Hi All,
below script reads the perticular files from the directory. Am trying to fetch status and print them in the required format.
It needs to read line and search for string "Passed/Failed" and print them under correct sub header.
Thanks bipinajith. It works perfectly.
Can you please tell me how to check for string Passed/Failed and color the string based on condition. ie,
if s/$1 ==Passed, then string color should be in Green otherwise Red.
I tried something but didn't work, all the text comes in green color.
Code:
for file in *_Daily_Check*
do
server=${file%%_*}
if [ -f "$file" ]
then
nawk -F":" -v S="$server" '/VerifyInterface/ {
#printf "|%s%20s", S, $1;if ($1 == "Passed"){$1 = "\033[1;32m" s "\033[0m" };if (s == "Failed"){$1 = "\033[1;31m" s "\033[0m"}
printf "\033[1;32m|%s%20s\033[0m", S, $1;if (s == "Passed"){$1 = "\033[1;32m" s "\033[0m" };
} /CoreFileGenerated/ {
printf "\033[1;32m%15s\033[0m", $1;
} /VerifyRegister/ {
printf "\033[1;32m%15s\033[0m", $1;
} /VerifyComponent/ {
printf "\033[1;32m%15s\033[0m", $1;
} /VerifyServiceAvailability/ {
printf "\033[1;32m%15s\033[0m|\n", $1;
} ' "$file"
echo "|--------------------------------------------------------------------------------------------------------|"
fi
done
}
bipinajith's code works flawlessly, still it might be affected by the sequence of occurrence of the keywords. If that is certain to remain the same all over, fine. If not, try
Code:
awk 'BEGIN {CmpAr["VerifyInterface"]=2 # some definitions
CmpAr["CoreFileGenerated"]=3
CmpAr["VerifyRegister"]=4
CmpAr["VerifyComponent"]=5
CmpAr["VerifyServiceAvailability"]=6
split ("%-7s %36s %22s %29s %26s %29s", FMT, " ")
red="\033[1;31m"
grn="\033[1;32m"
rst="\033[0m"
clr["Failed"]=red
clr["Passed"]=grn
}
function prt() {for (i=0; i<=6; i++) printf FMT[i], clr[OutAr[i]] OutAr[i] rst; printf "\n"}
NR == FNR {print; next} # read & print header
($2 in CmpAr) {gsub (" ", "", $1);
OutAr[CmpAr[$2]]=$1; # put $1 into the right column
OutAr[1]=$3} # save server name
FILENAME != oldfn { # print if old file ends
if (oldfn) prt(); oldfn = FILENAME}
END {prt()} # print when last file ends
' FS=":" header ????_Daily_Check.log
Server Interface Checks Process Checks Service Check
VerifyInterface Corefile VerifyRegister VerifyComponent
------- ---------------- --------- ------------- ------------- -------------
D01B Passed Passed PassedFailed Failed
E01A FailedPassedFailed Failed Failed
As you will infer, the header file contains what you need in the first lines of output.
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.
@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
Server Interface Checks Process Checks Service Check
VerifyInterface Corefile VerifyRegister VerifyComponent
------- ---------------- --------- ------------- ------------- -------------
into extra file called "header" and supply as first file to the script. It will print it as is giving you control over what shold appear in the first lines of your output.
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)
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)
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)
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)
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)
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)
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)
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)