Trouble with passing variable to sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trouble with passing variable to sed
# 1  
Old 06-15-2012
Network Trouble with passing variable to sed

Here is my code

#!/bin/bash
username=gnowicki
sed '$s/$/ $username/' < sshd_config 1 <> sshd_config

what this is supposed to do is take the name gnowicki and put it at the end of the last line of the sshd_config and it works except not using the variable, if I put the name "gnowicki" where "$variable" is it puts gnowicki at the end of the line correctly but if I put "$username" it will put "$username" in the sshd_config file and I only want what the variable equals.

what do I need to do? Smilie
# 2  
Old 06-15-2012
use " instead of '
# 3  
Old 06-15-2012
Code:
sed "$ s/$/ $username/" 

#note: the space after the first $

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Passing value of a variable in sed

Hi, I want to pass value of a variable track_line which is the line number to sed. Sed should print the lines starting from track_line till the last line of the file. I tried the below command but it is not working. sed -n '${track_line},$p' latest_log_file I tried using the below too but... (1 Reply)
Discussion started by: nitinupadhyaya8
1 Replies

2. Shell Programming and Scripting

sed not working while passing the variable to print

Hi I have below sed to search and print particular line that matches in file and prints it, which is actually working fine as expected. sed -n "/^2012-11-19 23:*/p" filename but if I replace date with the variable it seems it's not recognizing it and throwing out error v="2012-11-19"... (4 Replies)
Discussion started by: manas_ranjan
4 Replies

3. Shell Programming and Scripting

Trouble with sed and substituting a string with special characters in variable

Hey guys, I know that title is a mouthful - I'll try to better explain my struggles a little better... What I'm trying to do is: 1. Query a db and output to a file, a list of column data. 2. Then, for each line in this file, repeat these values but wrap them with: ITEM{ ... (3 Replies)
Discussion started by: ampsys
3 Replies

4. Shell Programming and Scripting

Trouble with passing Variable from bash to awk gsub command

Would really appreciate it if someone could point out my mistake in this line of code, i've been staring blankly at it trying everything i can think of some time now and coming up with nothing. #!/bin/bash echo "Enter Username" read Username awk -F: -v var=${Username} '/^var:/... (9 Replies)
Discussion started by: Nostyx
9 Replies

5. Shell Programming and Scripting

Passing a variable to sed command

Hi guys, I wanted to pass a variable to the sed command which tells which line to be deleted. a=2; echo $a; sed '$ad' c.out it is throwing an error. sed: 0602-403 "$a"d is not a recognized function. I even tried "$a" and \$a.. but it is of no use. Can you please correct me... (6 Replies)
Discussion started by: mac4rfree
6 Replies

6. Shell Programming and Scripting

Passing Variable in sed

Dear All, I want to print a file. First I tried with this sed '2q;d' filename it worked. But when i put following it is not working x=2; sed '$xq;d' filename Would any one suggest how to pass the variable? (7 Replies)
Discussion started by: saifurshaon
7 Replies

7. Shell Programming and Scripting

assigning SED output to a variable = trouble!

i'm on a Mac running BSD unix. i have a script in which i ask the user to input the name of a mounted volume. i then call SED to substitute backslashes and spaces in place of the spaces. that looks like this: echo "Enter the name of the volume" read Volume echo "You've chosen \"$Volume\""... (7 Replies)
Discussion started by: hungryd
7 Replies

8. Shell Programming and Scripting

Problem passing a specific variable to sed

Hi, I'm a bit of sed n00b here. My issue is as follows: I'm trying to pass a variable to sed so that all instances of this variable (in a text file) will be replaced with nothing. However, the value of this variable will always be a folder location e.g. "C:\Program Files\Folder1" I... (5 Replies)
Discussion started by: Mr_Plow
5 Replies

9. Shell Programming and Scripting

Passing a variable to sed or cut

:confused: Is it possible to send a variable to a sed or cut command? I have a test script as below: counter=1 while read line do # Test the file printf "$line" > temp$counter pref=$(cut c1-2000 $temp$counter | sed 's///g' | sed 's|.*PutTime\(.*)Origin.*|\1|') printf" let... (5 Replies)
Discussion started by: gugs
5 Replies

10. Shell Programming and Scripting

variable passing to sed

I m trying to pass variable to sed. export var=140920060731 sed -e '/$var/d' file but no luch so far..? any body has any idea abt it Is there any way to pass variable to SED? Thanks , Manish (2 Replies)
Discussion started by: Manish Jha
2 Replies
Login or Register to Ask a Question