Operating System - HP-UX
1849236 Members
2737 Online
104042 Solutions
New Discussion

comparing files using awk

 
Kurtkarl
Frequent Advisor

comparing files using awk

Hi,

I'm not a script guy and I'm currently in a situation which is, I need someone who could help me create a simple (for the experts) awk script that will compare system files against users input.

ex. - users entered "SAMPLE.TXT" (usually
upper case)
- systems could have "Sample.txt"
or "sample.txt"
or "sample.TXT"

regardless whatever the system has. Script should match users input against files in the system and proceed it's process. BTW, this is to ftp a file from one machine to another.

Your reply would be greatly appreciated.
Thanks
Joey
Just starting to learn thru this forum
6 REPLIES 6
Sridhar Bhaskarla
Honored Contributor

Re: comparing files using awk

One easy way is to use grep -i file_name
This will ignore case and pick up files.

For ex.,

DIR=/some_path
echo "Enter your file name:"
read file
ll $DIR |grep -i $file
if [ $? = 0 ]
then
echo $file is there
else
echo $file doesnot exist
fi

It will become little bit complicated if you use the path names.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Krishna Prasad
Trusted Contributor

Re: comparing files using awk

awk ' $0~/[S-s][A-a][M-m][P-p][L-l][E-e]/ ' filename

This will find any way the word sample was spelled in the filename searched.
Positive Results requires Positive Thinking
Anthony deRito
Respected Contributor

Re: comparing files using awk

One suggestion, use the toupper or tolower built-in funtions in awk to consider both character cases.

Tony
Robin Wakefield
Honored Contributor

Re: comparing files using awk

Hi Joey,

This may not be what you want, and it doesn't use awk, but it'll find all files under a directory, regardless of case, that match the user's input:

================================
#!/bin/ksh

DIR=search-directory

echo Enter filename: '\c'
read FILENAME

find $DIR -type f | grep -i "/${FILENAME}$"
==================================

Obviously, you need to edit the "DIR=" line.

Rgds, Robin. (duplicate of reply in 1st post!)
Vincent Farrugia
Honored Contributor

Re: comparing files using awk

Hello,

This script might help:

echo | ls -l | grep -i $1

If this script is named finder, then typing:

finder

should work for the current directory.

HTH,
Vince
Tape Drives RULE!!!
G. Vrijhoeven
Honored Contributor

Re: comparing files using awk

Hi,

#!/usr/bin/sh
if [ "$#" -ne 2 ]
then
echo "USAGE=script_name DIR_NAME FILE_NAME"
exit 1
fi
DIRNAME=$1
FILENAME=$2
for i in `find $DIRNAME -depth`
do
TEST=`echo $i | grep -v -i $FILENAME`
if [ "$TEST" -eq "" ]
then
echo $i
fi
done


Hope this will help,

Gideon