input text at certain lines of a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting input text at certain lines of a file
# 1  
Old 02-11-2011
input text at certain lines of a file

Hi,
I have an xml file which will be edited by the user.
I would like to get input from user and insert that at line 40 of the xml file.
PLease can some one help me to know how to insert the text at specified line using shell script.
# 2  
Old 02-11-2011
Code:
sed '40a '$input' ' xml

or

Code:
awk 'NR==40 {$0=$0"\n""'$input'"}1' xmlfile


Last edited by yinyuemi; 02-11-2011 at 04:00 AM..
# 3  
Old 02-11-2011
thanks a lot.
I would also want to replace the same text at line 35 to 40.
What i mean is there is already text between lines 45 to 50 and i want to replace that text.
Please cna you let me know how to achieve this (replacing text) ?

Thanks.
# 4  
Old 02-11-2011
Can you give a simple example for your requirement?
# 5  
Old 02-11-2011
Here is the example.
Below lines in the xml file start at line number 35 to 40.
I want to replace the 1.1.1.1 with another ip (provided by the user) and also the port number 389 with another port number


<module-option name="java.naming.provider.url">ldap://1.1.1.1:389/</module-option>
# 6  
Old 02-11-2011
Code:
ip=xxx
port=yyy
sed '35,40{s/\(.*:\/\/\)\([0-9].[0-9].[0-9].[0-9]\):\([0-9]\{3\}\)\(.*\)/\1'$ip':'$port'\4/}' xml

This User Gave Thanks to yinyuemi For This Post:
# 7  
Old 02-11-2011
thanks a lot. i will try this out.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Shell Programming and Scripting

Removing multiple lines from input file, if multiple lines match a pattern.

GM, I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed. I am assuming that sed, awk or even perl could do what I need. I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Discussion started by: jxfish2
7 Replies

3. Shell Programming and Scripting

Read n lines from a text files getting n from within the text file

I dont even have a sample script cause I dont know where to start from. My data lookes like this > sat#16 #data: 15 site:UNZA baseline: 205.9151 0.008 -165.2465 35.8109 40.6685 21.9148 121.1446 26.4629 -18.4976 33.8722 0.017 -165.2243 48.2201 40.6908 ... (8 Replies)
Discussion started by: malandisa
8 Replies

4. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

5. Shell Programming and Scripting

help with using text file as input

Hello All, I'm attempting to use a text file as input to a specific field in a command. Below is the command... Typically it looks like this ans_dump testzone.com channel=dnsw32 | grep AAAA I have about 500 zones I want to check... how do I use my text file as input where the zone name... (2 Replies)
Discussion started by: spartan22
2 Replies

6. Shell Programming and Scripting

how do i join two lines from a input file

HI all, i have a input file,i have to join 2nd line into firstline and 4th line into 2nd line and so on.. for this we also consider odd no. of line.It's operate on original input file but output file should temp file. like .. filename=cdr.cfg line1 line2 line3 line4Desired output should be... (9 Replies)
Discussion started by: suryanarayan
9 Replies

7. Shell Programming and Scripting

Extract the lines from input file

This is the sample input file b 05/Jul/2010:07:00:10 a 05/Jul/2010:06:00:10 b 05/Jul/2010:07:00:10 c 05/Jul/2010:07:10:10 d 05/Jul/2010:08:00:10 e 05/Jul/2010:09:00:10 f 05/Jul/2010:10:00:10 h 05/Jul/2010:11:00:10 i 05/Jul/2010:12:00:10 j ... (9 Replies)
Discussion started by: sandy1028
9 Replies

8. Shell Programming and Scripting

[bash help]Adding multiple lines of text into a specific spot into a text file

I am attempting to insert multiple lines of text into a specific place in a text file based on the lines above or below it. For example, Here is a portion of a zone file. IN NS ns1.domain.tld. IN NS ns2.domain.tld. IN ... (2 Replies)
Discussion started by: cdn_humbucker
2 Replies

9. UNIX for Dummies Questions & Answers

add new lines of text before and after each input line

I have a file that contains hundreds of lines such as: this_is_macro,000001 this_is_macro,000002 this_is_macro,000003 I would like to add the variable words MACROBEGIN MACRO_000001 MACROBEGIN MACRO_000002 MACROBEGIN MACRO_000003 above each line and add the word MACROEND ... (2 Replies)
Discussion started by: kenneth.mcbride
2 Replies

10. Shell Programming and Scripting

input text into file

hello everyone, this is my first time posting here so be nice ;-) I am a bit new at unix scripting and have basically been hacking other peoples scripts to get them to do what I need. I have now hit a bit of a stop. This problem is very basic but I can't just seem to figure out how to get... (1 Reply)
Discussion started by: cannonfodder
1 Replies
Login or Register to Ask a Question