Friday, August 2, 2013
Saturday, July 6, 2013
Illegal key size error
Problem : Application is not working and throwing an error "java.security.InvalidKeyException: Illegal key size or default parameters at javax.crypto.Cipher
....." in SystemOut.log .
Resolution:
if you use a key with an illegal key size that is not supported by default, this exception is thrown. To resolve this error, you must obtain the unrestricted strength cryptography policy files and install them.
....." in SystemOut.log .
Resolution:
if you use a key with an illegal key size that is not supported by default, this exception is thrown. To resolve this error, you must obtain the unrestricted strength cryptography policy files and install them.
The Keys refer to the encryption or decryption keys which are used in the Java Code.
You can migrate the unrestricted jurisdiction policy files, local policy.jar and US_export_policy.jar.
About this task
If you want to use encryption keys that are greater than 128-bits, you must use the unrestricted jurisdiction policy files, local_policy.jar and US_export_policy.jar.
The files are located in the [WAS_HOME/java/jre/lib/security] directory.
If your back-level version of WebSphere® Application Server is using the unrestricted jurisdiction policy files, you must perform special steps to migrate these files to your new version of WebSphere Application Server. If you are not using the unrestricted jurisdiction policy files, you do not need to take any action.
Procedure
1.Before migrating, copy the modified local_policy.jar file to a temporary location.
2.Migrate the WebSphere Application Server installation.
3.Copy the modified local_policy.jar file from step 1 to the following directory on the new WebSphere Application Server installation: WAS_HOME/java/jre/lib/security .
4.Start the new WebSphere Application Server installation as normal.
Depending on your choice of cipher strength (128, 192 or 256) for AES, you may need to add the unrestricted policy jar files to either $JAVA_HOME/jre/lib/security or $JAVA_HOME/lib/security. AES 128 does NOT require use of the policy files. These files are named: local_policy.jar and US_export_policy.jar.
You can migrate the unrestricted jurisdiction policy files, local policy.jar and US_export_policy.jar.
About this task
If you want to use encryption keys that are greater than 128-bits, you must use the unrestricted jurisdiction policy files, local_policy.jar and US_export_policy.jar.
The files are located in the [WAS_HOME/java/jre/lib/security] directory.
If your back-level version of WebSphere® Application Server is using the unrestricted jurisdiction policy files, you must perform special steps to migrate these files to your new version of WebSphere Application Server. If you are not using the unrestricted jurisdiction policy files, you do not need to take any action.
Procedure
1.Before migrating, copy the modified local_policy.jar file to a temporary location.
2.Migrate the WebSphere Application Server installation.
3.Copy the modified local_policy.jar file from step 1 to the following directory on the new WebSphere Application Server installation: WAS_HOME/java/jre/lib/security .
4.Start the new WebSphere Application Server installation as normal.
Depending on your choice of cipher strength (128, 192 or 256) for AES, you may need to add the unrestricted policy jar files to either $JAVA_HOME/jre/lib/security or $JAVA_HOME/lib/security. AES 128 does NOT require use of the policy files. These files are named: local_policy.jar and US_export_policy.jar.
The above steps also apply to BPM or process server upgrade.
Wednesday, June 26, 2013
playing mp3 on rhythmbox in RHEL
I executed two commands to enable mp3 play on my RHEL
# rpm -ivh http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
# yum -y install gstreamer-plugins-ugly gstreamer-plugins-bad
The above two commands did not work for rhythmbox but they worked well with Totem movie player.
You can also disable the repo by executing the below command
# mv /etc/yum.repos.d/rpmforge.repo /etc/yum.repos.d/rpmforge.repo.disabled
In case , you want to uninstall , use the following command
rpm -e gstreamer-plugins-bad
e stands for erase
Other useful commands
Search for a package
rpm -q -a|grep -i
Replace packagename with the package name you are aware of . -i will ignore the case while searching .
-q stands for query and -a stand for all . Basically this command is querying all the packages and then with the pipe and grep , we are trying the filter the output of rpm command .
Uninstalling opera browser was a difficult task for me because I was not able to find the package name for opera . I tried all the combinations to search for the opera package e.g rpm -q -a|grep -i opera etc.
Then it suddenly clicked to me to go to path where the executable for opera exist using the command
which opera
I found that there is a uninstall script for the opera as shown as below
[root@oc48857 bin]# ls -lrt *opera*
-rwxr-xr-x. 1 root root 161 May 2 02:32 opera
-rwxr-xr-x. 1 root root 34212 May 2 02:32 uninstall-opera
[root@oc48857 bin]# ./uninstall-opera
[root@oc48857 bin]#
This is how I was able to uninstall the opera . Thereafter I deleted.opera directory from all the User home directories .
[root@oc48857 ~]# rm -rf .opera
The objective of telling about the above exercise was to tell you that sometimes you may not be able to uninstall a software using rpm -e .Then you have to search for the uninstall script for that software created during the installation itself .
# rpm -ivh http://packages.sw.be/rpmforge-release/rpmforge-release-0.5.2-2.el6.rf.x86_64.rpm
# yum -y install gstreamer-plugins-ugly gstreamer-plugins-bad
The above two commands did not work for rhythmbox but they worked well with Totem movie player.
You can also disable the repo by executing the below command
# mv /etc/yum.repos.d/rpmforge.repo /etc/yum.repos.d/rpmforge.repo.disabled
In case , you want to uninstall , use the following command
rpm -e gstreamer-plugins-bad
e stands for erase
Other useful commands
Search for a package
rpm -q -a|grep -i
Replace packagename with the package name you are aware of . -i will ignore the case while searching .
-q stands for query and -a stand for all . Basically this command is querying all the packages and then with the pipe and grep , we are trying the filter the output of rpm command .
Uninstalling opera browser was a difficult task for me because I was not able to find the package name for opera . I tried all the combinations to search for the opera package e.g rpm -q -a|grep -i opera etc.
Then it suddenly clicked to me to go to path where the executable for opera exist using the command
which opera
I found that there is a uninstall script for the opera as shown as below
[root@oc48857 bin]# ls -lrt *opera*
-rwxr-xr-x. 1 root root 161 May 2 02:32 opera
-rwxr-xr-x. 1 root root 34212 May 2 02:32 uninstall-opera
[root@oc48857 bin]# ./uninstall-opera
[root@oc48857 bin]#
This is how I was able to uninstall the opera . Thereafter I deleted.opera directory from all the User home directories .
[root@oc48857 ~]# rm -rf .opera
The objective of telling about the above exercise was to tell you that sometimes you may not be able to uninstall a software using rpm -e .Then you have to search for the uninstall script for that software created during the installation itself .
Monday, May 20, 2013
Vmware installation on operating system with KVM enabled
Search for modules loaded in memory with string as kvm
lsmod |grep kvm
Remove those modules from kernel using the following command
modprobe -r kvm_intel
modprobe -r kvm
Install the rpm package which you have downloaded from www.vmware.com as per the operating system requirements.
rpm -i VMware-Player-2.5.5-328052.x86_64.rpm
Install the kernel-devel package from the configured repository in your system
yum -y install kernel-devel
Open up the VMware player, you will get a message that several modules must be compiled and loaded into the running kernel.Click on install.
You will get the same message again .This time , you click on cancel.
A window will open to open the Virtual Machine.
lsmod |grep kvm
Remove those modules from kernel using the following command
modprobe -r kvm_intel
modprobe -r kvm
Install the rpm package which you have downloaded from www.vmware.com as per the operating system requirements.
rpm -i VMware-Player-2.5.5-328052.x86_64.rpm
Install the kernel-devel package from the configured repository in your system
yum -y install kernel-devel
Open up the VMware player, you will get a message that several modules must be compiled and loaded into the running kernel.Click on install.
You will get the same message again .This time , you click on cancel.
A window will open to open the Virtual Machine.
Friday, May 3, 2013
Apache in a Nut Shell
httpd -l lists the static libraries
You may have to edit configure script to resolve an error while installing apache.
I have been getting an error because I am trying to install mod_ssl
I am running configure script and getting an error that shows the line number .
You must try to understand the error messages and see what is wrong.
Before installing apache make sure you have correct value of path variable. Installation will report error if something is wrong.
Edit your values ,properly.
Path variable needs to have proper kernel bin paths especially C compiler path.
What are you using CC or Gcc ?
If you are trying to reinstall apache then don't forget to clean the previous apache install using the make clean command .
sudo ./configure --prefix=/apps/apache_xyz --enable-mods-shared='cern-meta deflate disk-cache headers info mem-cache mime-magic isapi proxy rewrite speling unique-id usertrack vhost-alias' --enable-modules=mod-ssl --with-ssl=/usr/local/bin/openssl
You can use the command httpd -l for the second installation to list all statically compiled modules
There is also one more difference
first command is installing proxy and isapi modules
but the second command is not trying to install proxy and isapi modules.
Its better to install apache once again rather than adding a new module if you have a missing module.
You can't just copy and paste apache binaries ,they are dependent on your directory structure .
The two imporant files that store the configuration of apache are
httpd.conf and ssl.conf files.
How to get rid of passphrase while starting apache?
What do you get when you try to access a URL with http but at port 443?
E.g http://url:443
You may have to edit configure script to resolve an error while installing apache.
I have been getting an error because I am trying to install mod_ssl
I am running configure script and getting an error that shows the line number .
You must try to understand the error messages and see what is wrong.
Before installing apache make sure you have correct value of path variable. Installation will report error if something is wrong.
Edit your values ,properly.
Path variable needs to have proper kernel bin paths especially C compiler path.
What are you using CC or Gcc ?
If you are trying to reinstall apache then don't forget to clean the previous apache install using the make clean command .
sudo ./configure --prefix=/apps/apache_xyz --enable-mods-shared='cern-meta deflate disk-cache headers info mem-cache mime-magic isapi proxy rewrite speling unique-id usertrack vhost-alias' --enable-modules=mod-ssl --with-ssl=/usr/local/bin/openssl
sudo make clean
sudo make install
sudo ./configure --prefix=/apps/apache_xyz --enable-cern-meta --enable-cache --enable-disk-cache --enable-mem-cache --enable-cern-meta --enable-deflate --enable-expires --enable-headers --enable-info --enable-logio --enable-mime-magic --enable-rewrite --enable-so --enable-speling --enable-ssl --enable-unique-id --enable-usertrack --enable-vhost-alias --with-ssl=/usr/local
sudo make clean
sudo make install
What is the difference in the above two commands?
First command is trying to install shared libraries ( dynamic libraries)
There will be lines like LoadModule in the httpd.conf file for first installation.
Second command is trying to install libraries via static compiling .
You will not see LoadModule sentences in the httpd.conf file for second installationYou can use the command httpd -l for the second installation to list all statically compiled modules
There is also one more difference
first command is installing proxy and isapi modules
but the second command is not trying to install proxy and isapi modules.
Its better to install apache once again rather than adding a new module if you have a missing module.
You can't just copy and paste apache binaries ,they are dependent on your directory structure .
The two imporant files that store the configuration of apache are
httpd.conf and ssl.conf files.
How to get rid of passphrase while starting apache?
What do you get when you try to access a URL with http but at port 443?
E.g http://url:443
UNIX commands
To list disk usage
du -sg
du -sk
du -ksd in solaris to find the size of directory.
List all the directory usages in ascending order inside a particular directory in Solaris
du -sk *|sort -n
To get a formatted output on SunOS Solaris
df -F ufs -k
df -F ufs -k awk '{print $1}'
df -F ufs -k awk '{print $2}'
df -F ufs -k awk '{print $3}'
df -F ufs -k awk '{print $4}'
df -F ufs -k awk '{print $5}'
df -F ufs -k awk '{print $6}'
Print lines not containg #
crontab -l awk '!/#/'
Sum the fourth column
ls -lrt *2007*|awk '{ sum+=$5 }END{ print sum }'
Print the lines inside a tag e.g VirtualHost
awk '//' httpsd.conf
Using unix basic calculator
bc
scale=2
3/4
.75
^D to exit
Script to decrease the numeric contents of a file
var=`cat sample.txt`;
echo $var-1|bc > sample.txt
To list all processes that are using a file which has been deleted from a given file system(e.g. /usr) in AIX
fuser -d /usr
Below command to add two files in parallel line by line -w 200 adjusts the default length of each line to 200
sdiff -w 200 1.txt 2.txt > 3.txt
To find all directories
find . -type d
find directories owned by a user
find - -type d -user username
Creating directories, recursively
mkdir -p /1/2/3
To list all the commands that are given permission to u by root
sudo -l
Changing the comment of a user
sudo usermod -c "username,#CUST#,ABC,Systems Administrator,F" username
Getting the error "UX: usermod: ERROR: username is in use. Cannot change it."
Then you can manually edit the /etc/passwd if you have permissions either using sudo or root for changing comments.
How to read mails for a user in UNIX ?
mailx
type ? for help
type d * at command prompt to clear all the mails .
This is useful in order to clean up /var (will perform clean up of /var/mail)
Recursive grep
find . -type f -exec grep HOST {} \;
Delete files with whose name begins with -
rm ./-badfile
find files accessed in last one day
find . -atime -1
find files not accessed in last one day
find . -atime +1
Display all the process that are currently using that file.
fuser filename
To know the architecture type on solaris
isainfo -kv
E.g.isainfo -kv
64-bit sparcv9 kernel modules
To know the default permissions of current directory
ls -lad
ls -ld
Display the system configuration like RAM etc.
prtconf on Sun Solaris
lsconf on AIX
Perfomance monitoring commands
vmstat
iostat
top on solaris and linux
topas on AIX
sar
netstat
To display the operation system name and version
uname
uname -a
What is /dev ?
The /dev directory contains the special device files for all the devices
pwck , grpck are used for checking discrepancies in group and password files of solaris box.
The errors may not be that significant and they can be ignored even.
What are the files associated with name resolution on UNIX like systems ?
/etc/hosts
/etc/nsswitch.conf
/etc/resolv.conf
Where are Solaris System logs located ?
/var/adm/messages or you can run dmesg command to view the logs.
/etc/syslog.conf has the System logs configuration.
/var/crash/ .Its not a readable file and you need Solaris crash analysis tool
/var/log/syslog
du -sg
du -sk
du -ksd in solaris to find the size of directory.
List all the directory usages in ascending order inside a particular directory in Solaris
du -sk *|sort -n
To get a formatted output on SunOS Solaris
df -F ufs -k
df -F ufs -k awk '{print $1}'
df -F ufs -k awk '{print $2}'
df -F ufs -k awk '{print $3}'
df -F ufs -k awk '{print $4}'
df -F ufs -k awk '{print $5}'
df -F ufs -k awk '{print $6}'
Print lines not containg #
crontab -l awk '!/#/'
Sum the fourth column
ls -lrt *2007*|awk '{ sum+=$5 }END{ print sum }'
Print the lines inside a tag e.g VirtualHost
awk '//' httpsd.conf
Using unix basic calculator
bc
scale=2
3/4
.75
^D to exit
Script to decrease the numeric contents of a file
var=`cat sample.txt`;
echo $var-1|bc > sample.txt
To list all processes that are using a file which has been deleted from a given file system(e.g. /usr) in AIX
fuser -d /usr
Below command to add two files in parallel line by line -w 200 adjusts the default length of each line to 200
sdiff -w 200 1.txt 2.txt > 3.txt
To find all directories
find . -type d
find directories owned by a user
find - -type d -user username
Creating directories, recursively
mkdir -p /1/2/3
To list all the commands that are given permission to u by root
sudo -l
Changing the comment of a user
sudo usermod -c "username,#CUST#,ABC,Systems Administrator,F" username
Getting the error "UX: usermod: ERROR: username is in use. Cannot change it."
Then you can manually edit the /etc/passwd if you have permissions either using sudo or root for changing comments.
How to read mails for a user in UNIX ?
mailx
type ? for help
type d * at command prompt to clear all the mails .
This is useful in order to clean up /var (will perform clean up of /var/mail)
Recursive grep
find . -type f -exec grep HOST {} \;
Delete files with whose name begins with -
rm ./-badfile
find files accessed in last one day
find . -atime -1
find files not accessed in last one day
find . -atime +1
Display all the process that are currently using that file.
fuser filename
To know the architecture type on solaris
isainfo -kv
E.g.isainfo -kv
64-bit sparcv9 kernel modules
To know the default permissions of current directory
ls -lad
ls -ld
Display the system configuration like RAM etc.
prtconf on Sun Solaris
lsconf on AIX
Perfomance monitoring commands
vmstat
iostat
top on solaris and linux
topas on AIX
sar
netstat
To display the operation system name and version
uname
uname -a
What is /dev ?
The /dev directory contains the special device files for all the devices
pwck , grpck are used for checking discrepancies in group and password files of solaris box.
The errors may not be that significant and they can be ignored even.
What are the files associated with name resolution on UNIX like systems ?
/etc/hosts
/etc/nsswitch.conf
/etc/resolv.conf
Where are Solaris System logs located ?
/var/adm/messages or you can run dmesg command to view the logs.
/etc/syslog.conf has the System logs configuration.
/var/crash/
/var/log/syslog
Shell scripts
1)Arithmetic comparison and arithmetic addition
s=1;
while( [ $s -lt 100 ] )
do
echo $s;
s=$[$s + 2];
done
2)Read input
read input
echo "Welcome $input"
3)Display the first positional parameter
echo "Welcome $1"
4)Display the natural number from 1 to 50
for i in {1..50}do
echo $i;
done
5)Usage of if and arithmetic comparison operatorsread a;
read b;
if([ $a -lt $b ]) then
echo "X is less than Y";
fi
if([ $a -gt $b ]) then
echo "X is greater than Y";
fi
if([ $a -eq $b ]) then
echo "X is equal to Y";
fi
6)Usage of string comparison operators.
read input;
if ([ "$input" == 'y' ] || [ "$input" == 'Y' ]) then
echo "YES";
fi
if ([ "$input" == 'n' ] || [ "$input" == 'N' ]) then
echo "NO";
fi
7) Usage of logic operators and if condition.
read a;
read b;
read c;
if ( [ $a -eq $b ] && [ $b -eq $c ] ) then
echo "EQUILATERAL"
elif ( [ $a -eq $b ] || [ $b -eq $c ] || [ $c -eq $a ] ) then
echo "ISOSCELES"
else
echo "SCALENE"
fi
8)Below script shows the usage of bc to perform arithmetic operations. Scale = 4 directs bc to compute upto 4 decimal places and printf rounds the result to 3 decimal places. a is an arithmetic expression sent as an input to the script E.g. 2+2-5*6/4*7
read a;
b=`echo "scale=4;$a"|bc`
printf %.3f $b;
9)Below script takes n integers which is first input and then all the integers one by one .The task is to calculate the average . The script combines printf to round the result to 3 places and bc to calculate the average upto 4 decimal places . While loop is used to calculate the sum of numbers by iterating exactly n times.
read n;
i=0;
s=0;
while ([ $i -lt $n ])
do
read input;
s=$[$s+$input];
i=$[$i+1];
done
average=`echo "scale=4;$s/$n"|bc`;
printf %.3f $average;
10) Display the 3rd character of each line given as input .
cut -c 3
E.g. echo "Hello World"|cut -c 3 displays l as output .
cut -c 2,7 will display the 2nd and 7th character of the lines given as input.
cut -c 2-7 will display from 2nd to 7th character of the lines given as input.
cut -c 13- will display from 13th character to the end of the line.
11) Display first 3 fields of input delimited by tab. TAB is the default delimiter for cut.
f specifies the fields . Here, first to third field are displayed.
cut -f 1-3
cut -d " " -f 4 displays fourth field delimited by single space. cut -d " " -f 1-3 displays first to third field delimited by single space.
cut -f 2- displays second to last field delimited by tab.
12)Below script displays first 20 lines of a file.
i=1;
while ( [ $i -lt 21 ])
do
read input;
echo $input;
i=$[$i+1];
done
head -c 12 . Displays first 12 characters of a file.
head -n 20 . Displays first 20 lines of file
head -n 20|tail -n +12 . Displays 12th to 20th line of file
tail -n -20 . Displays last 20 lines of file.
tail -c 20 . Displays last 20 characters of a file.
13)If you want to write the name of all the Queue Manager to a single file to create a stop/start script for WebSphere MQ
dspmq|cut -d'(' -f1|cut -d')' -f1 > strmq.sh
14)
Removing control M characters from several files at a time in UNIX platforms
find . -name *.sh -exec perl -pi -e 's/^M//g' {} \;
Make sure to type control M character , do not copy and paste control M character .
You have to type control M by pressing ctrl v and ctrl m one after another
then check again for control M character with the below command
find . -name *.sh -exec grep -l "^M" {} \;
15)
If you want to remove control M character from a single file
perl -pi -e 's/^M//g' FILE NAME
16)Below script replaces ( to [ and ) to ]. Take a note that we have only used -p not -pi . When you use i , command expects you to provide an input file name .
perl -p -e 's/\(/\[/g' |perl -p -e 's/\)/\]/g'
17)Replace 2 or more spaces with one .
perl -p -e 's/ +/ /g'
18) sort . Sorts on first column
sort -r. Sorts on first column in reverse order
sort -n. Sorts on numeric column. Only one numeric column is present.
sort -n -r . Sorts on numeric column in reverse order. Only one numeric column is present
19)Below script converts the input
22) Copy a file specified on the input prompt from one location to another .If the file already exists in the other location ,then rename the file using date as suffix and then copy.
"$ cat testscript.sh
echo "Enter the name of file"
read file
echo $file
Date=`date|cut -f1 -d ' ' `
if(test -e $file)
then
mv $file $file$Date
cp TEST1/$file ./
echo $?
else
cp TEST1/$file ./
fi
$ echo $?
0
if [ $? -eq 0 ]
then
echo "Success"
fi
23)Script to check whether list of files written in a FILE 'TEMP1.txt' exist in directory inside which the script is run
for file in `cat filelist.txt`
do
if(test -e $file)
then
c=0
else
echo $file >> ListOfFiles.txt
fi
done
24)Find files containing a string and then replace string with new string in those files
find . -type f -exec cmd.sh "jdbc\/SomeDB" {} \;
OR
find . -type f -exec ./cmd.sh "jdbc\/SomeDB" {} \;
##contents of cmd.sh
grep $1 $2
if [ $? -eq 0 ]
then
echo $2 >> filenames.txt
fi
##Below script to replace
for file in `cat filenames.txt`
do
sed 's/old_string/new_string/g' $file > $file.new
mv $file.new $file
done
25)Merge files line by line in UNIX
paste file1 file2 > file3
Generic Numeric Sorting on second column seperated by tab in descending order
sort -t$'\t' -k2 -g -r
Generic Numeric Sorting on second column seperated by | in descending order
sort -t'|' -k2 -g -r
Generic Numeric Sorting on second column seperated by | in ascending order
sort -t'|' -k2 -g
Remove duplicates in standard input
Uniq
Counts and displays number of consecutive duplicates
uniq -c|xargs -l
Counts and displays number of consecutive duplicates and ignore cases
uniq -ic|xargs -l
Convert newline to tab from a standard input
awk 'BEGIN{ORS="\t";}{print;} END{}'
s=1;
while( [ $s -lt 100 ] )
do
echo $s;
s=$[$s + 2];
done
2)Read input
read input
echo "Welcome $input"
3)Display the first positional parameter
echo "Welcome $1"
4)Display the natural number from 1 to 50
for i in {1..50}do
echo $i;
done
5)Usage of if and arithmetic comparison operatorsread a;
read b;
if([ $a -lt $b ]) then
echo "X is less than Y";
fi
if([ $a -gt $b ]) then
echo "X is greater than Y";
fi
if([ $a -eq $b ]) then
echo "X is equal to Y";
fi
6)Usage of string comparison operators.
read input;
if ([ "$input" == 'y' ] || [ "$input" == 'Y' ]) then
echo "YES";
fi
if ([ "$input" == 'n' ] || [ "$input" == 'N' ]) then
echo "NO";
fi
7) Usage of logic operators and if condition.
read a;
read b;
read c;
if ( [ $a -eq $b ] && [ $b -eq $c ] ) then
echo "EQUILATERAL"
elif ( [ $a -eq $b ] || [ $b -eq $c ] || [ $c -eq $a ] ) then
echo "ISOSCELES"
else
echo "SCALENE"
fi
8)Below script shows the usage of bc to perform arithmetic operations. Scale = 4 directs bc to compute upto 4 decimal places and printf rounds the result to 3 decimal places. a is an arithmetic expression sent as an input to the script E.g. 2+2-5*6/4*7
read a;
b=`echo "scale=4;$a"|bc`
printf %.3f $b;
9)Below script takes n integers which is first input and then all the integers one by one .The task is to calculate the average . The script combines printf to round the result to 3 places and bc to calculate the average upto 4 decimal places . While loop is used to calculate the sum of numbers by iterating exactly n times.
read n;
i=0;
s=0;
while ([ $i -lt $n ])
do
read input;
s=$[$s+$input];
i=$[$i+1];
done
average=`echo "scale=4;$s/$n"|bc`;
printf %.3f $average;
10) Display the 3rd character of each line given as input .
cut -c 3
E.g. echo "Hello World"|cut -c 3 displays l as output .
cut -c 2,7 will display the 2nd and 7th character of the lines given as input.
cut -c 2-7 will display from 2nd to 7th character of the lines given as input.
cut -c 13- will display from 13th character to the end of the line.
11) Display first 3 fields of input delimited by tab. TAB is the default delimiter for cut.
f specifies the fields . Here, first to third field are displayed.
cut -f 1-3
cut -d " " -f 4 displays fourth field delimited by single space. cut -d " " -f 1-3 displays first to third field delimited by single space.
cut -f 2- displays second to last field delimited by tab.
12)Below script displays first 20 lines of a file.
i=1;
while ( [ $i -lt 21 ])
do
read input;
echo $input;
i=$[$i+1];
done
head -c 12 . Displays first 12 characters of a file.
head -n 20 . Displays first 20 lines of file
head -n 20|tail -n +12 . Displays 12th to 20th line of file
tail -n -20 . Displays last 20 lines of file.
tail -c 20 . Displays last 20 characters of a file.
13)If you want to write the name of all the Queue Manager to a single file to create a stop/start script for WebSphere MQ
dspmq|cut -d'(' -f1|cut -d')' -f1 > strmq.sh
14)
Removing control M characters from several files at a time in UNIX platforms
find . -name *.sh -exec perl -pi -e 's/^M//g' {} \;
Make sure to type control M character , do not copy and paste control M character .
You have to type control M by pressing ctrl v and ctrl m one after another
then check again for control M character with the below command
find . -name *.sh -exec grep -l "^M" {} \;
15)
If you want to remove control M character from a single file
perl -pi -e 's/^M//g' FILE NAME
16)Below script replaces ( to [ and ) to ]. Take a note that we have only used -p not -pi . When you use i , command expects you to provide an input file name .
perl -p -e 's/\(/\[/g' |perl -p -e 's/\)/\]/g'
17)Replace 2 or more spaces with one .
perl -p -e 's/ +/ /g'
18) sort . Sorts on first column
sort -r. Sorts on first column in reverse order
sort -n. Sorts on numeric column. Only one numeric column is present.
sort -n -r . Sorts on numeric column in reverse order. Only one numeric column is present
19)Below script converts the input
A 25 27 50
B 35 37 75
C 75 78 80
D 99 88 76
to output
A 25 27 50 : FAIL
B 35 37 75 : FAIL
C 75 78 80 : B
D 99 88 76 : A
awk 'BEGIN{} {sum=$2+$3+$4;mean=sum/3;if( mean >= 80 ) print
$1,$2,$3,$4,":","A"; else if( mean >= 60 )print $1,$2,$3,$4,":","B";
else if( mean >= 50 ) print $1,$2,$3,$4,":","C"; else print
$1,$2,$3,$4,":","FAIL";} END{}'
20)
Below script converts the input
A 25 27 50
B 35 37 75
C 75 78 80
D 99 88 76
to
A 25 27 50;B 35 37 75
C 75 78 80;D 99 88 76
awk '{if(NR%2==0) printf $0"\n"; else printf $0";";}'
Note that when you change the printf to print the output changes to
A 25 27 50;
B 35 37 75
C 75 78 80;
D 99 88 76
8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR
https://www.thegeekstuff.com/2010/01/8-powerful-awk-built-in-variables-fs-ofs-rs-ors-nr-nf-filename-fnr/?ref=binfind.com/web
21)
grep "[t,T]he "
searches for the line containing string "the"
grep -v "[t,T]hat " displays lines which do not contain that .
grep -e "[t,T]he " -e "[t,T]hat " -e "[t,T]hen " -e "[t,T]hose " searches for the words the, that ,then , those
22) Copy a file specified on the input prompt from one location to another .If the file already exists in the other location ,then rename the file using date as suffix and then copy.
"$ cat testscript.sh
echo "Enter the name of file"
read file
echo $file
Date=`date|cut -f1 -d ' ' `
if(test -e $file)
then
mv $file $file$Date
cp TEST1/$file ./
echo $?
else
cp TEST1/$file ./
fi
$ echo $?
0
if [ $? -eq 0 ]
then
echo "Success"
fi
23)Script to check whether list of files written in a FILE 'TEMP1.txt' exist in directory inside which the script is run
for file in `cat filelist.txt`
do
if(test -e $file)
then
c=0
else
echo $file >> ListOfFiles.txt
fi
done
24)Find files containing a string and then replace string with new string in those files
find . -type f -exec cmd.sh "jdbc\/SomeDB" {} \;
OR
find . -type f -exec ./cmd.sh "jdbc\/SomeDB" {} \;
##contents of cmd.sh
grep $1 $2
if [ $? -eq 0 ]
then
echo $2 >> filenames.txt
fi
##Below script to replace
for file in `cat filenames.txt`
do
sed 's/old_string/new_string/g' $file > $file.new
mv $file.new $file
done
25)Merge files line by line in UNIX
paste file1 file2 > file3
Generic Numeric Sorting on second column seperated by tab in descending order
sort -t$'\t' -k2 -g -r
Generic Numeric Sorting on second column seperated by | in descending order
sort -t'|' -k2 -g -r
Generic Numeric Sorting on second column seperated by | in ascending order
sort -t'|' -k2 -g
Remove duplicates in standard input
Uniq
Counts and displays number of consecutive duplicates
uniq -c|xargs -l
Counts and displays number of consecutive duplicates and ignore cases
uniq -ic|xargs -l
Convert newline to tab from a standard input
awk 'BEGIN{ORS="\t";}{print;} END{}'
Thursday, May 2, 2013
C for Conflicts
For some people ,it meant nothing, for some it meant something and for some it meant everything.
He was the one ,for whom it meant everything and when he came to realize that for others it meant nothing,he felt like dead.His head burnt like fire and the conflicts started in his little mind.
Conflicts about what happened to him was wrong or what he did was wrong or the world is wrong or what others say is wrong.And here started the world of skepticism in him.Arguing about the things..arguing about everything..arguing against even about science ..arguing against even about the fundamentals of society.
Saturday, January 5, 2013
Non recursive implementation of mergesort
It took me a while to implement the mergesort algorithm . The algorithm was an easier one to understand but a difficult one to write .
Here is a great video on youtube which explains the mergesort
http://www.youtube.com/watch?v=GCae1WNvnZM
The question is why did I write a non-recursive program not a recursive one. I would say I have always hated recursion because of my inability to comprehend it .Whenever I try to understand recursion , I get limited only to factorial of a number like program or a fibonacci sequence number . I am never able to completely understand the complicated recursive programs . I always feel that this is something that I would never be able to think of while writing a program .So for me recursion is a technique which if you can implement, then that is well and good and if you can not, then think of a non recursive approach which is more fun.
Below is the code of program.
/*Author:Ashish Sharma
The program does the sorting of integers using MergeSort algorithm
I have not assigned any variables for Math functions used in the program so as to make the program look easy
The following test cases are covered
1.Count of numbers given as input to the program is a number equal to Math.pow(2,k)
2.Count of numbers given as input to the program is a number not equal to Math.pow(2,k)
3.Same number is repeated multiple times in the input
4.Count of input numbers is odd in number */
import java.util.*;
import java.lang.Math;
public class MergeSort {
public static void main (String args[])
{
/*Take the input into ArrayList using scanner*/
ArrayList InputArrayList = new ArrayList();
System.out.println("Give the input array, It will take the input until you press a non numeric key");
Scanner scan = new Scanner(System.in);
while(scan.hasNextInt())
{
InputArrayList.add(scan.nextInt());
}
System.out.println("ArrayList is " + InputArrayList);
/*Convert the ArrayList to IntegerArray */
int i, SIZE=InputArrayList.size();
int[] InputArrayInt= new int[SIZE];
int[] OutputArrayInt= new int[SIZE];
for(i=0;i{InputArrayInt[i]=InputArrayList.get(i);}
/*pass this array to mergesort function for sorting and collect it to OutputArrayInt*/
OutputArrayInt=mergesort(InputArrayInt);
/*Display the output array*/
System.out.println("The sorted array is ");
for(i=0;iSystem.out.println(OutputArrayInt[i]);
}
public static int[] mergesort(int[] IAInt)
{
int lenth= IAInt.length;
int i, count,k,l,m,n,temp,index=0;
int[] OAInt=new int[lenth];
/*Initially the each element is taken as a subarray when k=0,
The array is divided into subarrays of two elements each when k=1,
Then , the array is divided into subarrays of four elments each when k=2;
and so on..
The i sets a limit to the length , inside which there are two subarrays of Math.pow(2,k) length which are merged .
So, the merging at any time is done for two subarrays the one that begins at i+(even number)*Math.pow(2,k) which is l and the one that begins
i+(odd number )*Math.pow(2,k) which is m
The importance of while loop inside if conditions is when l and m have reached their maximum limit and you want to just put values to an output array from a sorted array and increment index and l or m.
Count is the maximum number of operations that needs to be done while merging two sorted arrays.*/
for(k=0;k<=Math.floor(Math.log(lenth)/Math.log(2));k++)
{
for(i=0;i{
for(count=1,l=i,m=i+(int)Math.pow(2,k);count <=(Math.pow(2,k+1));count++)
{
//System.out.println("The values of k, i,l ,m and count are "+k+" "+i+" " +l+" "+m+" "+count);
if(index{
if(IAInt[l]<=IAInt[m] && l< i+Math.pow(2,k) && m {
OAInt[index]=IAInt[l];
l=l+1;
index=index+1;
if(l==i+Math.pow(2,k) )
{
while(m{
OAInt[index]=IAInt[m];
index=index+1;
m=m+1;
count=count+1;
}
}
}
else if(IAInt[l]>IAInt[m] && l< i+Math.pow(2,k) && m {
OAInt[index]=IAInt[m];
m=m+1;
index=index+1;
if(m==i+2*Math.pow(2,k)||m==lenth)
{
while(l{
OAInt[index]=IAInt[l];
index=index+1;
l=l+1;
count=count+1;
}
}
}
}
/*This else condition is added for the case when m has exceeded the length of the array while assigning value to m in the count loop */
else if (index=lenth )
{
while(l < lenth)
{
OAInt[l]=IAInt[l];
l=l+1;
}
}
}//end of count loop
}//end of i loop
/*This for loop is to assign the sorted values to IAInt because mergesort operations are performed on IAInt.*/
for(n=0;n{
IAInt[n]=OAInt[n];
}
index=0;
}//end of k loop
/*The last return can be either of IAInt of OAInt because IAInt and OAint are the same at the end of completion of for loops.*/
return(OAInt);
}
}
Here is a great video on youtube which explains the mergesort
http://www.youtube.com/watch?v=GCae1WNvnZM
Below is the code of program.
/*Author:Ashish Sharma
The program does the sorting of integers using MergeSort algorithm
I have not assigned any variables for Math functions used in the program so as to make the program look easy
The following test cases are covered
1.Count of numbers given as input to the program is a number equal to Math.pow(2,k)
2.Count of numbers given as input to the program is a number not equal to Math.pow(2,k)
3.Same number is repeated multiple times in the input
4.Count of input numbers is odd in number */
import java.util.*;
import java.lang.Math;
public class MergeSort {
public static void main (String args[])
{
/*Take the input into ArrayList using scanner*/
ArrayList
System.out.println("Give the input array, It will take the input until you press a non numeric key");
Scanner scan = new Scanner(System.in);
while(scan.hasNextInt())
{
InputArrayList.add(scan.nextInt());
}
System.out.println("ArrayList is " + InputArrayList);
/*Convert the ArrayList to IntegerArray */
int i, SIZE=InputArrayList.size();
int[] InputArrayInt= new int[SIZE];
int[] OutputArrayInt= new int[SIZE];
for(i=0;i
/*pass this array to mergesort function for sorting and collect it to OutputArrayInt*/
OutputArrayInt=mergesort(InputArrayInt);
/*Display the output array*/
System.out.println("The sorted array is ");
for(i=0;i
}
public static int[] mergesort(int[] IAInt)
{
int lenth= IAInt.length;
int i, count,k,l,m,n,temp,index=0;
int[] OAInt=new int[lenth];
/*Initially the each element is taken as a subarray when k=0,
The array is divided into subarrays of two elements each when k=1,
Then , the array is divided into subarrays of four elments each when k=2;
and so on..
The i sets a limit to the length , inside which there are two subarrays of Math.pow(2,k) length which are merged .
So, the merging at any time is done for two subarrays the one that begins at i+(even number)*Math.pow(2,k) which is l and the one that begins
i+(odd number )*Math.pow(2,k) which is m
The importance of while loop inside if conditions is when l and m have reached their maximum limit and you want to just put values to an output array from a sorted array and increment index and l or m.
Count is the maximum number of operations that needs to be done while merging two sorted arrays.*/
for(k=0;k<=Math.floor(Math.log(lenth)/Math.log(2));k++)
{
for(i=0;i
for(count=1,l=i,m=i+(int)Math.pow(2,k);count <=(Math.pow(2,k+1));count++)
{
//System.out.println("The values of k, i,l ,m and count are "+k+" "+i+" " +l+" "+m+" "+count);
if(index
if(IAInt[l]<=IAInt[m] && l< i+Math.pow(2,k) && m {
OAInt[index]=IAInt[l];
l=l+1;
index=index+1;
if(l==i+Math.pow(2,k) )
{
while(m{
OAInt[index]=IAInt[m];
index=index+1;
m=m+1;
count=count+1;
}
}
}
else if(IAInt[l]>IAInt[m] && l< i+Math.pow(2,k) && m {
OAInt[index]=IAInt[m];
m=m+1;
index=index+1;
if(m==i+2*Math.pow(2,k)||m==lenth)
{
while(l{
OAInt[index]=IAInt[l];
index=index+1;
l=l+1;
count=count+1;
}
}
}
}
/*This else condition is added for the case when m has exceeded the length of the array while assigning value to m in the count loop */
else if (index
{
while(l < lenth)
{
OAInt[l]=IAInt[l];
l=l+1;
}
}
}//end of count loop
}//end of i loop
/*This for loop is to assign the sorted values to IAInt because mergesort operations are performed on IAInt.*/
for(n=0;n
IAInt[n]=OAInt[n];
}
index=0;
}//end of k loop
/*The last return can be either of IAInt of OAInt because IAInt and OAint are the same at the end of completion of for loops.*/
return(OAInt);
}
}
Subscribe to:
Posts (Atom)