I am making a script in which.I have a problem i try to discribe.
File1 content need to be append in file 2 but such as if content already exist then dont add.
File1 IS Like this
myname
yourname
hername
hisname
File2
%AddNameHere myname yourname hername hisname
The file2 layout that you show does not reflect what you stated as "append" the name to the file, but I'm assuming append (meaning at the end of the file). I'm also assuming that each name is on a separate line in file2. Considering those assumptions, here is what I came up with for an example in bash:
#!/bin/bash
#
# check arguments
#
if (( $# != 2 ))
then
echo ""
echo $0 : Invalid number of arguments $#
echo ""
exit 1
fi
#
# declare variables (easier read)
#
declare file1=$1
declare file2=$2
#
# read each line in file1, check if it exists in
# file2, and if not, then append it.
#
cat $file1 | \
while read name
do
egrep -s $name $file2
if (( $? != 0 ))
then
echo $name >> $file2
echo "added name "$name
fi
done
let me know if this does it, or if my assumptions are wrong.
To: cfajohnson... thanks for taking the time to fix this up. I'll try and keep your advice in mind next time. ( Guess I should get your book, huh? :>D )
To: aliahsan81... just for completeness, here is the same script with cfajohnson suggestions.
#!/bin/bash
#
# check arguments
#
if [ $# -ne 2 ]
then
echo ""
echo $0 : Invalid number of arguments $#
echo ""
exit 1
fi
#
# declare variables (easier read)
#
file1=$1
file2=$2
#
# read each line in file1, check if it exists in
# file2, and if not, then append it.
#
while read name
do
egrep -s $name $file2
if [ $? != 0 ]
then
echo $name >> $file2
echo "added name "$name
fi
done < $file1