Sed - Appending a line with a variable on it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed - Appending a line with a variable on it
# 1  
Old 07-30-2007
Sed - Appending a line with a variable on it

Hi,

I searched the forum for this but couldn't find the answer. Basically I have a line of code I want to insert into a file using sed. The line of code is basically something like "address=1.1.1.1" where 1.1.1.1 is an IP Address that will vary depending on what the user enters. I'll just refer to it here as $address.

I did find something about append or insert with sed using this line:

sed '1a\
your text goes here' file_name > new_filename

How would this line look if I have a variable in the line? Any ideas? Thanks!

Elt
# 2  
Old 07-30-2007
first of all where do you want to insert the text? append to the end of the file or insert it in the first line?

the code that you are trying would insert the text after the first line.
# 3  
Old 07-30-2007
You can't put a variable inside a single-quotes string. So break the single-quoted string into two single-quoted strings and put the variable between them. For an example see our FAQ article on this subject.
# 4  
Old 07-30-2007
Hey thanks guys. Actually what I was trying to do is have a script insert a line into a file with address=[some ip address] where the ip is something that the user inputs. It's supposed to be appended to a line which happens to be line 77. What I did was split my sed call into 2 so the first time it appends the address= and then the next one it appends the ip address which can vary. I was surprised that sed needs to be on different lines though, kinda interesting. Anyways I got it, but can someone link me to the FAQ for future problems. Thanks!

Elt
# 5  
Old 07-30-2007
Code:
sed '77{p;s/.*/Your_String/;}' input_file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed for appending a line before a pattern and after another patter.

Hi All, I'm appending a line before a pattern and after another pattern(;) but omitting the first pattern - same as if comes duplicates before the second pattern. Also, I'm adding a word before the first pattern - Here is my file select blah blah hello select blah blah ; select... (6 Replies)
Discussion started by: Mannu2525
6 Replies

2. UNIX for Dummies Questions & Answers

Appending sed output to variable

I want to append matched output and cat the results into an variable. but I've been running into problems. sed is printing result on to screen instead of appending the output to $CAPTURE. I'm stumped...how should i fix this? contents of $TEST 10.0.0.1 10.0.0.2 10.0.0.3 10.0.0.4 expected... (5 Replies)
Discussion started by: jazzaddict
5 Replies

3. Shell Programming and Scripting

Appending string (charachters inside the line) to a fixed width file using awk or sed

Source File: abcdefghijklmnop01qrstuvwxyz abcdefghijklmnop02qrstuvwxyz abcdefghijklmnop03qrstuvwxyz abcdefghijklmnop04qrstuvwxyz abcdefghijklmnop05qrstuvwxyz Whatever characters are in 17-18 on each line of the file, it should be concatenated to the same line at the character number... (6 Replies)
Discussion started by: tamahomekarasu
6 Replies

4. Shell Programming and Scripting

awk;sed appending line to previous line....

I know this has been asked before but I just can't parse the syntax as explained. I have a set of files that has user information spread out over two lines that I wish to merge into one: User1NameLast User1NameFirst User1Address E-Mail:User1email User2NameLast User2NameFirst User2Address... (11 Replies)
Discussion started by: walkerwheeler
11 Replies

5. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

6. UNIX for Dummies Questions & Answers

use variable for sed print line

I'm using the sed command to pull a line from a file and place it into another file: sed -n '1p' students >tmp this pulls the first line, I want to change the 1 to a variable which will be a counter to go through the rest of the lines but can't get it. I've tried: sed -n '$cp students... (3 Replies)
Discussion started by: atchleykl
3 Replies

7. Shell Programming and Scripting

sed: appending alternate line after previous line

Hi all, I have to append every alternate line after its previous line. For example if my file has following contents line 1: unix is an OS line 2: it is open source line 3: it supports shell programming line 4: we can write shell scripts Required output should be line1: unix is an OS it is... (4 Replies)
Discussion started by: rish_max
4 Replies

8. UNIX for Dummies Questions & Answers

SED or AWK: Appending a line Identifier that changes with paragraph?

Have another question that has been eluding me all day. I have data file I'm trying to reformat so that each line is appended with an ID code, but the ID code needs to update as it searches through the file. I.e. ----Begin Original Datafile----- Condition = XXX Header Line 1 Header... (1 Reply)
Discussion started by: selkirk
1 Replies

9. Shell Programming and Scripting

Appending line with sed works on Linux but not on Solaris

Hi folks, Our application installation uses "sed" command to append string after specific line or after line number. Both cases work perfect on Linux but fail on Solaris. The OS versions are Solaris 9 and Linux Red Hat AS 3. i.g: Linux: ----- file foo.txt aaa bbb ccc ddd root#... (4 Replies)
Discussion started by: nir_s
4 Replies

10. Shell Programming and Scripting

Appending line/lines with sed

Hi folks, I need to append line or bulk of lines into a file. For example,I have the following section in opmn.xml file: <process-type id="OC4J_RTEadmin_NIR" module-id="OC4J"> <module-data> <category id="start-parameters"> <data... (28 Replies)
Discussion started by: nir_s
28 Replies
Login or Register to Ask a Question