replacing a string in a file with command line parameter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replacing a string in a file with command line parameter
# 1  
Old 07-27-2006
replacing a string in a file with command line parameter

Hello,
I am trying to replace a string with a paramter given along with the script.
I am replacing application1 to application2 with the script:
./change_app.sh application2

change_app.sh:
#!/bin/ksh
grep $1 applications.dat 2>&1 >/dev/null
echo $1
file=pckage.new
sed 's/Name: application1/Name: ${1}/g' "$file" > ./tmpfile
mv ./tmpfile "$file"

applications.dat has all the applications

But I get output as ${1} instead of application2. But when I echo $1, I get application2.
Please help in where I am going wrong, or if there is any better way.

Thanks
# 2  
Old 07-27-2006
sed 's/Name: application1/Name: ${1}/g' "$file" > ./tmpfile

should be:

sed 's/Name: application1/Name: '${1}'/g' "$file" > ./tmpfile
# 3  
Old 07-27-2006
Oops.. Thank you.
Quick question:
What do I have to do if I don't know the application1 name, but have to change - say a string next to Name: with application2.

sed 's/Name: application1/Name: '${1}'/g' "$file" > ./tmpfile

How do I refer application1 globally ?
# 4  
Old 07-28-2006
You can do something like this (assume application name is alphanumeric) :
Code:
sed -e "s/Name: [[:alnum:]]\{1,\}/Name: ${1}/g" "$file" > ./tmpfile

Jean-Pierre.
# 5  
Old 08-02-2006
I have a Version Name: 1.0.0_A0
I have to change to 1.1.0_A1
With the script
sed -e "s/Name: [[:alnum:]]\{1,\}/Name: ${1}/g" "$file" > ./tmpfile

The output I am getting is 1.1.0_A10.0_A0

I think its stopping when it sees '.'
Can you please tell me what do I need to do for special characters ??

Thanks
# 6  
Old 08-03-2006
The following characters have been added : - . _
Code:
sed -e "s/Name: [-[:alnum:]._]\{1,\}/Name: ${1}/g" "$file" > ./tmpfile

The character - must be specified first.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

delete the line with a particular string in a file using sed command.

Hello, I want to delete all lines with given string in file1 and the string val is dynamic. Can this be done using sed command. Sample: vars="test twinning yellow" for i in $vars do grep $i file1 if then echo "Do Nothing" else sed `/$i/d` file1 fi done Using the above... (5 Replies)
Discussion started by: PrasadAruna
5 Replies

2. Shell Programming and Scripting

perl script - command line parameter

i am a beginner, i want to make a program that takes any command line arguments... and print it out in reverse. ie. if the command line argument is "thanks for helping me" i want it to output "me helping for thanks" :D i have tried using the reverse command, but i cant get it working!! ... (3 Replies)
Discussion started by: bshell_1214
3 Replies

3. Shell Programming and Scripting

Remove line based on string and put new line with parameter

Hi Folks, I am new to ksh, i have informatica parameter file that i need to update everyday with shell script. i need your help updating this file with new parameters. sample data $$TABLE1_DATE=04-27-2011 $$TABLE2_DATE=04-23-2011 $$TABLE3_DATE=03-19-2011 .......Highligned... (4 Replies)
Discussion started by: victor369
4 Replies

4. Shell Programming and Scripting

How to get the last command line parameter?

"$#" gives the number of command-line arguments. How do you get the last command-line parameter (or any particular one determined by a variable)? I thought it would be "${$#}", but that produces something completely unexpected. (4 Replies)
Discussion started by: dkarr
4 Replies

5. Shell Programming and Scripting

Replacing a string with new line

Hi, Can anyone help me know how to replace a string with the new line for ex: file1 val1 or val2 or val3 or I need to replace the "or" with new line. Thanks in advance (2 Replies)
Discussion started by: mr_manii
2 Replies

6. UNIX for Dummies Questions & Answers

replacing string in a column on a specific line

hi, i currently have a file with columns similar to this customer name owed CID123 John 300 CID342 harry 500 at present i use use awk to find the amount owed by the customer using the customer ID (CID). if the customer spends more money how would i go about using sed/awk etc to... (2 Replies)
Discussion started by: skinnygav
2 Replies

7. Shell Programming and Scripting

error while replacing a string by new line character in sed

hi, when i am doing the following things getting error Can anyone please suggest i have a file where there is a line like the following branch=dev sdf dev jin kilii fin kale boyle dev james dev i want to search the existance of dev in the above line. cat "$file" | sed -n... (8 Replies)
Discussion started by: millan
8 Replies

8. Shell Programming and Scripting

Replacing a string in nth line

Hello All, How to replace a string in nth line of a file using sed or awk. For Ex: test.txt Line 1 : TEST1 TEST2 TEST3 Line 2 : TEST1 TEST2 TEST3 TEST4 Line 3 : TEST1 TEST2 TEST3 TEST5 Line 4 : TEST1 TEST2 TEST3 TEST6 Line 5 : TEST1 TEST2 TEST3 TEST7 i want to go to 4th line of a... (1 Reply)
Discussion started by: maxmave
1 Replies

9. Programming

Command line parameter for C program

I am writing a C program that part of the idea is to using a command line parameter to control not to run certain part of the sub program. I am totally new to C, I do not have any idea how to pass a command line arguments from a C program. Can anyone help ?! Thanks (3 Replies)
Discussion started by: Wing m. Cheng
3 Replies

10. UNIX for Dummies Questions & Answers

Command Line width parameter

can someone please tell me how i can increase the number of characters that can be input on the command line? (2 Replies)
Discussion started by: Scoogie
2 Replies
Login or Register to Ask a Question