Hello Experts,
I am working on a small file editing script. Since all experts here are very generous to give me the complete code, I would take up the problem in steps so that I ensure my opportunity to learn.
AIM: The script has some commented and some uncommented lines. I need to :
- Comment and uncomment lines as per requirement
- Change some lines, again as per requirement
- The requirement is specified by the command line arg
Here is what I have thought of:
- Place # in front of all uncommented lines.
- edit the required line
Suppose my file f.txt is:
#qwertyu
asdfgh
#zxcvbnm
qwertyuiop
The desired o/p is:
#qwertyu
#asdfgh
#zxcvbnm
#qwertyuiop
What I tried:
sed 's/^/#/g' f.txt > result.txt # first command
sed 's/^[^#]/#/g' f.txt > result.txt # second command
The problem is:
First command places a # in front of each line, not that it's a problem as I am simply concerned with commenting the lines, but I want to know if what I am trying is possible.
Second command replaces the first character too :(.
Please provide me some ideas to approach this problem.
If you think I am headed in the wrong direction, pls correct me and advise some other way to accomplish my task.
Kindly avoid direct code solutions, hints are most welcome.
Thank You.
Regards,
HKansal
Hello again,
This solves my first problem:
sed 's/^[^#]/#&/g' f.txt > result.txt
Now I am looking to edit a particular line, ll try it after having something to eat... hungry.
I'll get back to you all if have troubles
Still if you have any better approach to the problem, pls share.
Regards,
HKansal
Hello Experts,
Here is the final script:
#############################################################
##
## Shell : hDeploy
##
## Author : Himanshu Kansal
##
## Date : April 23, 2009 (Thursday)
##
## Description : The shell takes in the Properties File Name,
## Environment Name and Application Names as input
## and edits the OM.props file accordingly.
##
## Version : v0.01a
##
## Enhancements : It should be enhanced to take care of
## 1. Deployment steps.
## 2. New Initiatives/Applications.
##
## Usage : sh hDeploy <propsFile> <env> <app_1> [<app_2> ...]
##
## Notes : 1. All parameters should be correct for desired o/p
## 2. If a parameter value is incorrect, run again
## with correct values
## Dependencies : apps.txt - Contains mapping of application names with numbers.
##
## TO-DO : 1. Call cleanUp() wherever necessary. (Priority : low, Severity : Not Severe)
##
#############################################################
#!/bin/sh
################################################
##
## Function : getEnvPath
## Objective : Check if input env is valid
## Description : Initialise path value of EAR if valid,
## else exit with error message
## Date : April 23, 2009
## Author : Himanshu Kansal
##
################################################
getEnvPath(){
# Variables used :-
# ==============
# var : The environment argument passed
var=$1
case "$var" in
[CS]IT[1-4])
;;
PSDEV | PSSIT)
;;
*)
return 1
;;
esac
echo "/tmp/OM/OM_"${env}
}
################################################
##
## Function : getAppNum
## Objective : Get the number of application
## Description : Return App Number according to App
## Date : April 28, 2009
## Author : Himanshu Kansal
##
################################################
getAppNum(){
# Variables used :-
# ==============
# appName : Name of the application
appName=$1
## Check App Name and return 1 if NOT ok
findApp=`grep '^'"$appName"'=' ${appsFile}`
if [ ${#findApp} -eq 0 ];then
## App Name not found, return 1
return 1
fi
grep "${appName}" ${appsFile} | head -1 | awk -F"=" '{print $2}'
}
################################################
##
## Function : giveUsage
## Objective : Show the Shell Usage to user
## Description : Prints the valid usage syntax
## Date : May 19, 2009
## Author : Himanshu Kansal
##
################################################
giveUsage(){
echo "USAGE :"
echo "\tsh hDeploy <propsFile> <env> <app_1> [<app_2> ...]"
}
################################################
##
## Function : uncomment
## Objective : Un-comment a particular line
## Description : Un-comments the input line and saves the changes source
## file to destination file.
## Date : June 1, 2009
## Author : Himanshu Kansal
##
################################################
uncomment(){
# Variables used :-
# ==============
# lineToEdit : The line ofthe file to edit
# srcFile : Source file to edit
lineToEdit=$1
srcFile=$2
awk "/^#${lineToEdit}/{sub(/^#/,\"\")} 1" ${srcFile}
}
################################################
##
## Function : cleanUp
## Objective : Removes all the temporary files created
## Date : June 1, 2009
## Author : Himanshu Kansal
##
################################################
cleanUp(){
rm -rf htmp.props htmp2.props
}
# Variables used :-
# ==============
# env : Environment for deployment
# envPath : Path of the ear file for this environment
# propsFile : The properties file to edit
# appsFile : The file with number of apps
## Command-line Arguments:
## - $1 : propsFile
## - $2 : Environment
## - $3,... : Applications (Space separated values)
propsFile=$1
env=$2
appsFile="apps.txt"
## Check if Props file is mentioned
if [ "${#1}" = "0" -o ! -s ${propsFile} ];then
## No Props File Name specified
echo "\nA props file was not provided or File \"${propsFile}\" not found."
giveUsage
## Safely exit with non-zero code
exit 1
fi
## Get path for this environment
envPath=`getEnvPath "${env}"`
## Exit with message if environment is invalid
if [ $? -ne 0 ];then
echo "\nInvalid Env \"${env}\": Exiting"
giveUsage
## Safely exit with non-zero code
exit 1
fi
echo "Environment Path : ${envPath}"
## Forget propsFile and env args, move to other args.
## Check if an Application Name(Eg: OMEAR) is provided.
if [ "${#3}" = "0" ];then
## No App Name specified
echo "\nNo Application specified"
giveUsage
## Safely exit with non-zero code
exit 1
else
shift 2
fi
#echo "Props File : ${propsFile}"
#cat ./${propsFile}
## Comment all lines in the props file
#echo "[[ Commenting all lines]]"
sed "s/^[^#]/#&/g" ${propsFile} > htmp.props
#echo "[[ Commented all lines ]]"
## Now "htmp.props" is the operation file
## Loop through all the applications specified.
## Get the application number for an app
## Edit the required lines for that app
## Move to next app
## The numbers are stored in file "apps.txt" for easy edit/review/update
for app in $*
do
echo "${app}"
## Get app num
appNum=`getAppNum "${app}"`
#echo "[[App Num]] : ${appNum}"
## Check return value, exit if 1
if [ $? -ne 0 ];then
echo "Invalid Application : Skipping"
exit
fi
echo "[[App Num]] : ${appNum}"
## Edit the required lines
## Uncomment line for App Name
uncomment "Application\.${appNum}\.name" "htmp.props" > htmp2.props
## Edit line for App EAR location
sed "s~#Application\.${appNum}\.earFile=.*~Application\.${appNum}\.earFile=${envPath}/${app}.ear~g" htmp2.props > htmp.props
## Uncomment target cluster
#awk "/^#Application\.${appNum}\.target/{sub(/^#/,\"\")} 1" htmp.props > htmp2.props
uncomment "Application\.${appNum}\.target" "htmp.props" > htmp2.props
## Un-comment 2 more lines for SMG3TestingToolEAR
if [ ${app} = "SMG3TestingToolEAR" ];then
uncomment "ApplicationLibrary\.${appNum}\.appName" "htmp2.props" > htmp.props
uncomment "ApplicationLibrary\.${appNum}\.libraryName" "htmp.props" > htmp2.props
fi
## Update htmp.props for next application
cat htmp2.props > htmp.props
done
## Finally, save result to required file
mv htmp2.props ${propsFile}
echo "File ${propsFile} edited."
cleanUp
exit 0
#####################################################
## End Of File - hDeploy ##
#####################################################
Please feel free to review and post comments.
Thank You.
Regards,
HKansal