replace information in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace information in a file
# 1  
Old 12-07-2010
replace information in a file

I need to change the ntp server on 100 Unix servers and want to script it out. How do I replace data in a file? What I want is to change what is in bold

server ntpserver_old prefer

to this

server new_ntpservername prefer
# 2  
Old 12-07-2010
Code:
awk -v s=new_ntpservername '/^server/{$2=s}1' file

# 3  
Old 12-07-2010
Thanks. Is there any way to do that if there are several lines that begin with server?
# 4  
Old 12-07-2010
Assuming you don't have leading or trailing spaces around the lines:
Code:
sed '/^server .*prefer$/ s/ntpserver_old/new_ntpservername/' file

Otherwise:
Code:
sed '/^[ ]*server .*prefer[ ]*$/ s/ntpserver_old/new_ntpservername/' file

# 5  
Old 12-07-2010
There are many ways to do that, one of them is for instance if you know the number of line you want to update:
Code:
$ cat -n file
     1  #test
     2  server ntpserver_old prefer
     3  server ntpserver_old prefer

Code:
$ awk -v s=new_ntpservername 'NR==2 && /^server/{$2=s}1' file
#test
server new_ntpservername prefer
server ntpserver_old prefer

# 6  
Old 12-07-2010
set ssh with keyless on these 100 unix servers with root account, make sure you can login by ssh without password.

test by below command

Code:
$ cat server.list
server1
server2

$ cat server.list |xargs -i ssh {} uname -n

test below code on one server first, if it is successful, add all servers in file server.list
Code:
while read server
do
  ssh $server perl -i.bak`date +%Y%m%d` -pe 's/ntpserver_old/new_ntpservername/'  /etc/inet/ntp.conf
done < server.list


Last edited by rdcwayx; 12-07-2010 at 07:43 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search for the information at file

I'm having few question. i'm have a input file. Other information CONNECTIONS "BP-COLLECTOR" J6.4 "BP-TEST".4; +5VS C34.1 U21.1; DEV_I2C_SDA J6.6 R4.1 U18.1; DEVICES "BP-TEST" 1."BP-LED_ANODE" (8 Replies)
Discussion started by: kttan
8 Replies

2. Shell Programming and Scripting

How to create file and file content based existing information?

Hi Gurus, I am SQL developer and new unix user. I need to create some file and file content based on information in two files. I have one file contains basic information below file1 and another exception file file2. the rule is if "zone' and "cd" in file1 exists in file2, then file name is... (13 Replies)
Discussion started by: Torhong
13 Replies

3. Shell Programming and Scripting

How to extract information a file according key id in another file?

hi, i have a large file containing the detailed information of a bunch of keys like this: JAT_0001 contig102_342_3_n2 contig102_342 atgcacgacta 30 50 20... (11 Replies)
Discussion started by: the_simpsons
11 Replies

4. UNIX for Dummies Questions & Answers

Grep? - using a file of terms to search another file when the information is on a different line

I have a flat file that looks like this, let's call it Chromosome_9.txt: FT /Gene_Name="Guanyl-Acetylase 9" FT /Gene_Number"36952" FT /Gene_Name="Endoplasmic Luciferase" FT /Gene_Number"36953" FT ... (4 Replies)
Discussion started by: Twinklefingers
4 Replies

5. Shell Programming and Scripting

reading a file extracting information writing to a file

Hi I am trying to extract information out of a file but keep getting grep cant open errors the code is below: #bash #extract orders with blank address details # # obtain the current date # set today to the current date ccyymmdd format today=`date +%c%m%d | cut -c24-31` echo... (8 Replies)
Discussion started by: Bruble
8 Replies

6. Shell Programming and Scripting

Create shell script to extract unique information from one file to a new file.

Hi to all, I got this content/pattern from file http.log.20110808.gz mail1 httpd: Account Notice: close igchung@abc.com 2011/8/7 7:37:36 0:00:03 0 0 1 mail1 httpd: Account Information: login sastria9@abc.com proxy sid=gFp4DLm5HnU mail1 httpd: Account Notice: close sastria9@abc.com... (16 Replies)
Discussion started by: Mr_47
16 Replies

7. Shell Programming and Scripting

Copying Information from One File to Another File in Shell

Hello, I'm new to scripting and I need help moving text from one file to another file. Here are examples what the files look like. File 1: Ac-223 2.10m A 1 0 0 0 Fr-219 358 9.9000E-01 0 0.0 0 0.0 0 0.0... (1 Reply)
Discussion started by: tamachan414
1 Replies

8. Shell Programming and Scripting

Only the required tag information the XML file file

Hi i have a single line xml file having many account no tag, from which i need only the account no from the tag. any one can help on this. below is the xml file: ... (2 Replies)
Discussion started by: Saravanapk
2 Replies

9. UNIX for Dummies Questions & Answers

getting information from a file

I have a file which is of the format :- /Ixxxxxx/WORK/HANDOFF;/xxxxxx/WORK/MB_BACKUP;<servername>;<dest-servername>;\\\\CAICMESF002DATA_GRP\OPERATIONS\IBBSSPOOLS\HANDOFF\;ACHOFF*;achoff;binary;3;1;|ACHOFF.zip;0;1;0; I want to grep all capital and small OFF pattern in a format :- ... (2 Replies)
Discussion started by: kamlesh_k
2 Replies

10. UNIX for Dummies Questions & Answers

File/directory information......

Basically i have done created the script below, and it functions ok, it prints the access rights the user has. But i need it to print the group permissions, and other permissions, it would also be helpful if i could print the permissions in numeric form aswell, if it is possible. I have looked in... (50 Replies)
Discussion started by: Makaveli.2003
50 Replies
Login or Register to Ask a Question