help needed with shell script to append to the end of a specific line in a file on multiple servers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help needed with shell script to append to the end of a specific line in a file on multiple servers
# 1  
Old 06-09-2011
help needed with shell script to append to the end of a specific line in a file on multiple servers

Hi Folks,

I was given a task to append three IP's at the end of a specific (and unique) line within a file on multiple servers.

I was not able to do that with the help of a script. All I could was:

for i in server1 server2 server3 server4
do
ssh $i
done

I know 'sed' could be used to append to the end of the line but how to save that and replicate on all servers in the list?

Original File
-----------

#cat /etc/ftpd/ftpaccess
greeting brief
log commands
log transfers
class users real 172.25.6.148 172.25.197.148 172.25.52.100 172.21.56.75 172.21.54.159 172.25.2.25 172.25.2.26 172.25.119.132 172.25.119.133 172.25.119.134 172.25.119.135 172.25.183.132 172.25.183.133 172.25.183.134 172.25.183.135
banner /etc/ftpd/banner.txt

(each server had different IP's in the line starting with: class
users real .....):

Change required:
----------------

Append "172.21.105.99 172.21.105.100 172.21.105.101" at the end of line starting with text:'class users real')


Any help will be appreciating in how would you gurus would accomplish this task with the help of a script / scripted-command.

Thanks in advance
# 2  
Old 06-10-2011
Not sure I understand, but this might work if I do.
Code:
for i in server1 server2 server3 server4; do
    ssh $i "sed -i 's/\$/ 172.21.105.99 172.21.105.100 172.21.105.101/' file.txt"
done

Note: the backslash protects the dollar sign from the local shell. When it arrives on the remote server, it's in single quotes. The double quotes and backslash have been removed.
# 3  
Old 06-10-2011
Quote:
Originally Posted by KenJackson
Code:
for i in server1 server2 server3 server4; do
    ssh $i "sed -i 's/\$/ 172.21.105.99 172.21.105.100 172.21.105.101/' file.txt"
done

this will append the ip address in all the lines.
# 4  
Old 06-10-2011
Oops. I left off the address.
Code:
for i in server1 server2 server3 server4; do
    ssh $i "sed -i '/class users/s/\$/ 172.21.105.99 172.21.105.100 172.21.105.101/' file.txt"
done

# 5  
Old 06-10-2011
Code:
ssh $i "sed -i ' /^class users real/ s/\$/ 172.21.105.99 172.21.105.100 172.21.105.101/' file.txt"

# 6  
Old 06-13-2011
Thanks for both the replies and making it work for me!
Salute! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Needed shell script to append desired text to each line in a file

Hi, I had generated a report in my tool as followsoutput.txt 43.35 9 i needed the script to generate a new file like below i want to append the text to each of these lines of my filenewoutputfile.txt should be Total Amount : 43.35 Record Count:9 Regards, Vasa Saikumar. ... (2 Replies)
Discussion started by: hemanthsaikumar
2 Replies

2. Shell Programming and Scripting

SED and Solaris Append line to the end of File does not work

Hello, I have to add a new line at the end of a File on Solaris-System: I think my script should be right, because I evaluated it to other threads. However the script does not what I am expected it should do. My file might look like this: Line1 Line2 Line3 And my script could... (7 Replies)
Discussion started by: Timo_HR
7 Replies

3. Shell Programming and Scripting

Append the end of each line in a file with a given string

Hi friends, I have a file containing many lines as follows. M:\mmarimut_v6.4.0_pit_01\java\build.xml@@\main\v6.4.0_pit_a M:\mmarimut_v6.4.0_pit_01\ADBasicView.java@@\main\v6.4.0_pit_a I would like to append the string "\0" at the end of each line in the file. The output should look... (10 Replies)
Discussion started by: nmattam
10 Replies

4. Shell Programming and Scripting

Append a string at the end of every line in a file

Hi Friends, I have a file with many lines as shown below. /START SAMPLE LINE/ M:\mmarimut_v6.4.0_pit_01\java\build.xml@@\main\v6.4.0_pit_a M:\mmarimut_v6.4.0_pit_01\port\Post.java@@\main\v6.4.0_pit_a M:\mmarimut_v6.4.0_pit_01\switchview\View.java@@\main\v6.4.0_pit_a /END SAMPLE LINE/ I... (1 Reply)
Discussion started by: nmattam
1 Replies

5. Shell Programming and Scripting

How to append value at first line of CSV file using shell script?

I have an issue where I need to append a value at the last of the csv, I have created a shell script and it is appending the columns at the last but it is appending at all lines, and my requirement is specific to just append at the 1st line. Have a look and suggest, (7 Replies)
Discussion started by: anujrichhariya
7 Replies

6. Shell Programming and Scripting

shell script to read a line in gps receiver log file and append that line to new file

Hi, I have gps receiver log..its giving readings .like below Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. GPSD,R=1 $GPGSV,3,1,11,08,16,328,40,11,36,127,00,28,33,283,39,20,11,165,00*71... (3 Replies)
Discussion started by: gudivada213
3 Replies

7. Shell Programming and Scripting

How can I append a string at the end of a line in a file

Hi, guys. I have one question: I have a file called "group", the contents of it is below: ******************************** ... test:x:203: sales:x:204: repair:x:205: research:x:206:brownj ... *********** Now I want to add string ",sherrys" at the end of "research:x:206:brownj", so... (5 Replies)
Discussion started by: daikeyang
5 Replies

8. Shell Programming and Scripting

Line Count and Append it to the end of the file.

Hi, I want to get a Line count of a file and append that at the end of the file. The Line count should not include the Headers : ------------------ COL1,COL2,COL3 123,abc,011 111,abd,0212 Record Count: 2 ------------------- Thanks. (7 Replies)
Discussion started by: smc3
7 Replies

9. Shell Programming and Scripting

append a character at end of each line of a file

Hi, i want to append a character '|' at end of each line of a file abc.txt. for example if the file abc.txt conatins: a|b|c 1|2|33 w|2|11 i want result file xyz.txt a|b|c| 1|2|33| w|2|11| I know this is simple but sumhow i am not able to reach end of line. its urgent, thanks for... (4 Replies)
Discussion started by: muaz
4 Replies

10. Shell Programming and Scripting

Append text at end of the first line in a file

Hi I need to append some text @ end of the first line in a file. like myfile.txt list = a,b,c list.a=some.. I give the arg "d" . now it append at end of first line list=a,b,c,d list.a=some... Please help me out this (7 Replies)
Discussion started by: catgovind
7 Replies
Login or Register to Ask a Question