Hello ,
Using the below scrip to search a string in a file , by case-insensitively
Please assist on using the toupper() as getting error !.
#!/usr/bin/ksh
set -x
curr_dir=`pwd`
file_ctr=0
printf "\n Reviewing the output file from the directory: %s \n\n" $curr_dir
ls -latr $cur_dir *[Aa][Ww]*[Ss][Rr]*[Dd][Mm][Ll]*.lst
for SQLOutFile in `ls $cur_dir *[Aa][Ww]*[Ss][Rr]*[Dd][Mm][Ll]*.lst`;
do
file_ctr=` expr $file_ctr + 1 `
printf "\n %d : Processing the file %s \n" ${file_ctr} ${SQLOutFile}
echo $0
starttime=`awk '/START TIME/ {x=NR+5;next}(NR==x){print}' toupper($SQLOutFile) `
echo $starttime
printf "\t Start Time is : %s %s \n" ` awk '/START TIME/ {x=NR+5;next}(NR==x){print}' ${SQLOutFile} | cut -d ' ' -f3,4 `
done;
Error:
./test.sh
+ ./test.sh[17]: syntax error at line 1 : `(' unexpected
Thanks Siva SQL
You are getting error since toupper() should be used inside awk. Why do you use toupper() ? (You are already fetching filenames in upper or lower case)
In addition to what anbu23 has already said, note that if the file name you are processing contains any lowercase letters, converting those lowercase letters to uppercase would cause awk
to fail with an error noting that no file with the converted filename exists.
I have to search perform on the file content into case-insensitive search; that's why using toupper()
Please be much more explicit about what you are trying to do. The awk toupper()
function converts lowercase letters in the string it is given as an argument to uppercase letters. It does not change characters found inside a file whose name is specified by its operand. And, as stated before, toupper()
is an awk
function, not a shell utility.
If you want to convert the contents of a file to change lowercase letters within a file to uppercase letters while preserving the original ownership, access modes, and links you can use either of the following:
TmpFile=${0##*/}.$$
awk '{print toupper($0)}' "$SQLOutFile" > "$TmpFile" &&
cp "$TmpFile" "$SQLOutFile" &&
rm "$TmpFile"
If you're trying this on a Solaris/SunOS system, change awk
to /usr/xpg4/bin/awk
or nawk
.
or:
TmpFile=${0##*/}.$$
tr '[:lower:]' '[:upper:]' < "$SQLOutFile" > "$TmpFile" &&
cp "$TmpFile" "$SQLOutFile" &&
rm "$TmpFile"
If what you want is to perform a case insensitive match when looking for the string START TIME
, but you want the output produced by your awk
script to match the case sensitive input found in the file, change:
starttime=`awk '/START TIME/ {x=NR+5;next}(NR==x){print}' toupper($SQLOutFile) `
to:
starttime=$(awk 'toupper($0) ~ /START TIME/ {x=NR+5;next}(NR==x)' "$SQLOutFile")
or to:
starttime=$(awk '/[Ss][Tt][Aa][Rr][Tt] [Tt][Ii][Mm][Ee]/ {x=NR+5;next}(NR==x)' "$SQLOutFile")