Showing posts with label for loop in unix. Show all posts
Showing posts with label for loop in unix. Show all posts

Saturday, September 13, 2008

Taking count of all files , where file names were passed as parameters from another file

Taking a count of all files listed in a diirectory
=================================

#! /bin/ksh
cd /home/manu
for i in `cat file1.txt`
do
echo "$i" | cat $i | wc -l
done

Removing a particular prefix from all files listed by ls

Removing a particular prefix from all file names listed by ls -lrt e.g. dbo.manu.sql-->manu.sql ......
------------------------------------------------------------------------

#! /bin/ksh
for i in `ls *.sql`
do
NEW=`echo $i | sed -e 's/^dbo.//g'`
mv ${i} ${NEW}
done

Comparing two columns

#! /bin/ksh

########################
### Created on : 10-sep-2008 ###
### Created By : Manu Swami ###
#########################

############## How to use -- USAGE ###############

usage="$0 File1 File2"

############ Setting up the environment variables #####################

datetime=`date '+%Y%m%d'`
export FILE1="$1"
export FILE2="$2"

########## If either of the mandatory parameters is missing raise error #############

if [[ "$FILE1" = "" ]] || [[ "$FILE2" = "" ]] then
print "Invalid usage, Usage is : "
print "$usage"
exit 1
fi
############################################################

echo "\n"
echo "#### Contents of "$1" which are not present in "$2" ####"
for i in $(cat "$1")
do

cat "$2" | grep "^"$i"$">check.txt

if [ -s check.txt ]
then
cat check.txt>>result.txt
else
echo "Not found --> "$i" : in the file "$2".."
fi

done

echo "\n"
echo "#### Contents of "$2" which are not present in "$1" ####"
for j in $(cat "$2")
do

cat "$1" | grep "^"$j"$">checkf.txt

if [ -s checkf.txt ]
then
cat checkf.txt>>result.txt
else
echo "Not found --> "$j" : in the file "$1".."
fi

done

echo "\n"
echo "Cleaning up temporary files, if there is any...."
rm result.txt
rm check.txt
rm checkf.txt
echo "Temp Section cleaned."

echo "\n"

Friday, September 12, 2008

Replace semicolon with @ in a new line

#!/bin/ksh
for i in `ls *.sql`
do
cat $i | sed '/^$/d' | sed '/;/G' | sed 's/;//g'| sed 's/^$/@/g' >> manu.txt
done