Replace line in files using script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace line in files using script.
# 1  
Old 05-25-2010
Power Replace line in files using script.

First sorry for my bad english.
I have search the forum without finding anything I fully understand, so I ask here.

The problem:

I have a config file that I will chance onece a day. One line only.
I know the linenumber, and I also know the start of the line.
Line number 19, and the start of the line is "H: 10.0.0.9 60110 user1 user1"

I want to replace the whole line with a predefine variable $newline, the rest I have figured out for my self, but now I am stuck...

So... How do I locate the linenumber or the known start of the line, and how do I replace that line with a new line stored in a variable, then save and close the file...

The filename is "debsvr.cfg" and is located in "/var/etc/" on a debian server.

I am pretty new at shell scripting, so if you can please explain so my kid will understand it :P

Many thanks in advance Smilie
# 2  
Old 05-25-2010
Hi, try this:

Code:
sed -i "19s/.*/$newline/" /var/etc/debsvr.cfg

sed is a stream editor. Since your using debian it will probably have the -i (in place) option. This command goes to line 19 and replaces everything on that line with the content of shell variable $newline and write the result back (in place) to the same file.
# 3  
Old 05-25-2010
try:

Code:
sed "19s/.*/$newline/" debsvr.cfg

with awk:
Code:
awk -v c="$newline" '{ if(NR==19) { print c} else {print $0} } ' debsvr.cfg

# 4  
Old 05-25-2010
Many many thanks Smilie))
I tried Scrutinizer's suggestion and it's works 100% together with the rest of the script Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read each line in fileA, replace in all files with matching filename

Hello, I supposed that I asked the similar question but I could not have found it when I search on our forum. EXYU+: EXYU%3A+HRT+1 EXYU%3A+HRT+2 EXYU%3A+HRT+3 EXYU_url: #SERVICE 1:0:1:0:0:0:0:0:0:0:http%3A//xxxx%3Ayyyy@mmmm%3A55555/stream/channelname/YYYY?profile=htsp7777.ts #SERVICE... (2 Replies)
Discussion started by: baris35
2 Replies

2. Shell Programming and Scripting

Replace values in script reading line by line using sed

Hi all, Let's say I have a script calling for the two variables PA_VALUE and PB_VALUE. for pa in PA_VALUE blah blah do for pb in PB_VALUE blah blah do I have a text file with two columns of values for PA and PB. 14.5 16.7 7.8 9.5 5.6 3.6 etc etc I would like to read this... (7 Replies)
Discussion started by: crimsonengineer
7 Replies

3. UNIX for Dummies Questions & Answers

Replace lines of two files by the corresponding line numbers.

I want to replace lines. The files 1 are (separated by \t) Gm01 phytozome9_0 three_prime_UTR 70641 70759 . - . ID=PAC:26323927.three_prime_UTR.1;Parent=PAC:26323927;pacid=26323927 Gm01 phytozome9_0 three_prime_UTR 90230 90692 . - . ... (1 Reply)
Discussion started by: grace_shen
1 Replies

4. UNIX for Dummies Questions & Answers

Script to find line in one file and replace in another

Hey Guys, im looking for a script that will work under OSX. What i want to do is copy information from one file (Specific LIne) and write it to a certain line in another. To be more specific... I want the hostname of a mac to be gathered ( i assume its stored in a .plist file somewhere) and... (2 Replies)
Discussion started by: padgo
2 Replies

5. Shell Programming and Scripting

awk replace first line of files

Hi, I am very new to scripting and I know this is a very basic question, but I couldnt find a solution online or make it work. I need to search all my directories and subdirectories for files named run_* and replace the first line if some pattern is found. Here is my first attempt, which... (2 Replies)
Discussion started by: andlessa
2 Replies

6. UNIX for Dummies Questions & Answers

Replace line via perl script of file

Hi All, I wanted to do following. 1) If file exit then open file for reading and check if my string is there then i wanted to replace the entire line with that string + something else then close the file. 2) If file does not exit then i need to open the file and write to it. I am done with... (0 Replies)
Discussion started by: Tarun24
0 Replies

7. Shell Programming and Scripting

script to replace numbers on lines according to condition on the same line

hello everyone my file contains many records, the following is a sample: BEGIN ASX1500000050002010120000000308450201012000177 ASX1100002000000201012000000038450201012000220 ASX1600100005000201012000000038450020101200177 ASX1900100006000201067000000058450020101200177... (2 Replies)
Discussion started by: neemoze
2 Replies

8. Shell Programming and Scripting

how can i replace / with new line in shell script or sed ?

1. how can i replace ' / ' with new line in shell script or sed ? 2. how can set a conditon untill null in while loop while ( i== null ) do ...... done (3 Replies)
Discussion started by: mail2sant
3 Replies

9. Shell Programming and Scripting

Search and replace multi-line text in files

Hello I need to search for a mult-line text in a file exfile1 and replace that text with another text. The text to search for is in exfile2 and the replacement text is in exfile3. I work with kornshell under AIX and need to do this with a lot of files. (the file type is postscript and they need... (10 Replies)
Discussion started by: marz
10 Replies

10. UNIX for Advanced & Expert Users

how to replace a line in a file using shell script

I have a property file in which the DB name is specified and when i run my servers they will point to the DB specified in that property file. Now i'm gonna write a script which will start all the services. But before that i just want to dynamically change the DB name in that property file by... (3 Replies)
Discussion started by: cs_sakthi
3 Replies
Login or Register to Ask a Question