Thursday, August 19, 2010

Unix: Edit/Add string into a file

Here I have given useful scripts for editing and append some text into a file.

This command used to get the line number of a word in the file.

#grep -n "word" asd1.sh | cut -f1 -d:

To get the output in a variable use the below command:

#VAR='grep -n "word" asd1.sh | cut -f1 -d:'

Below command will add 1 to result line. bc is the command line calculator.

#echo `grep -n "word" asd1.sh | cut -f1 -d:` + 1 | bc


Command to add text in a file at given location

#awk 'NR==3{print "newline"}1' asd1.sh


test.sh

findline=`grep -n "log4j:configuration" jboss-log4j.xml | cut -f1 -d:`
addline=`echo $findline+1| bc`
sed ""${addline}"i\ $appender" jboss-log4j.xml > test.txt


The above command will be useful for adding the appender to Jboss log4j xml file during deployment.

RPM Relocatable

Relocatable package is give control to installer for installing in a perferred location. A RPM software package install files in /usr/share by default but user may want to install package in /etc/ location then specifying the relocatable will be useful for the users.
All the RPM are not relocatable and it is just a vendor giving control or option for installing in perferred locations. Most package are not relocatable.
To make it a package as relocatable you should add some scripts in SPEC file.

Below line has to written in Preamble/introduction section of RPM SPEC file.

Prefix: dir

Given file installating in /usr/share and /etc are relocatable.
Prefix: /usr/share
Prefix: /etc

RPM will install as like given in %files section. You can override with "--prefix dir". For example, if your package is going to install in /etc directory, you can install package in /usr/share by "rpm --prefix /usr/share"

For the multiple relocation, use --relocate argument as rpm.

rpm . --relocate /etc/=/usr/share --relocate /var=/usr