Adding extra word from file1 to file2

I need to add a word from file1 to file2 accordinggly... file1 contains name of servers and file2 version of server

I need that information in a single file so that the format is

server_name : version

I been trying but havent been able to figure out how to search for a file using sed...

PLEASE HELP!!

if we do not know how your file1 and file2 look like, we cannot help to parse it.

paste file1 file2 >mergedfile

???

file1                    file2
word1            sentence1
word2            sentence2
word3            sentence3

and i need to end in separate file or file1 or 2, whatever is easier in this format

word1;sentence1
word2;sentence2
man paste
paste -d";" file1 file2
1 Like

what about if i have my file1 in this format
word1
word2
word3

and my file2 is always going to be in this format
sentece2

so I need a for loop so that whenever a file2 file3 filex comes will add that 1 sentence to my file1. Or is it just easier to paste with 2 separate files.

Thank you,

All depends on what you have and what you want.

I don't understand what kind of things you have nor what kind of this you want to achieve, i need more infos as well as full example of input you have and output you want.

I may then be able to help you further.

ok...so here is my script

# Logs to each server using file, gets relase information into a file, copies
#it to harp server, deletes file.
for host in `cat $PATH_TEMP/name_of_file`
do
ssh $host sh lsb_release -a >> $host/tmp/$host_release.info
scp -p $host:/tmp/$host_release.info $PATH_TEMP/release.info
rm /tmp/$host_release.info
done

So when the script is executed for the fist time...this will be the result in the local host

list_file
server1
server2
server3

server1_file
version

final_file
server1;version

if we keep going this is what I want to have in the final_file
list_file
server1
server2
server3

server2_file
version

final_file
server1;version
server2;version

and the only thing that will change as the script uses the list file would be the serverX file, which willl have a different version and that will be combined with the 2nd entry of file 1 to be entered in final_file as the second entry.

Does that help

Did you test your script ?
What error message do you get ?

1. for host in `cat $PATH_TEMP/name_of_file`
2. do
3. ssh $host sh lsb_release -a >> $host/tmp/$host_release.info
4. scp -p $host:/tmp/$host_release.info $PATH_TEMP/release.info
5. rm /tmp/$host_release.info
6. done

In line 3 of your code shouldn't you have a colon after $host ($host:/tmp/$host_release.info)?
In line 5 of your code shouldn't the rm occure on the remote $host (ssh $hosts rm /tmp/$host_release.info)?

thanks for the replies...
first of all, do I need

$host:

every time I perform a command in the remote server.

Also, do I need to ssh everytime I execute a command on the server..

This is my current script and It runs but the file does not come as I need it, however if I execute the commands manually on the remote servers, the file is created correctly.

# Logs to each server using file and append the name of file with the release description into
# a single file and copies it to harp server
echo "entering for loop"
for host in `cat $PATH_TEMP/test1.txt`
    do
echo "inside do loop"
        #logs to server and gets release information into file
echo "before ssh"
        ssh $host more /etc/redhat-release >> $host:/tmp/host_release.info
echo "done creating release file"
        #creates file and adds name of server to the file
        touch $host:/tmp/host_name.info
echo "done creating file with server name"
        echo $host >> /tmp/host_name.info
echo "done modifying server name file"
        #concatenates both files into a single file with a ';'
        paste -d ";" $host:/tmp/host_name.info $host:/tmp/host_release.info >> $host:/tmp/release.info
echo "done formatting file"
        scp -p /tmp/release.info harp:$PATH_TEMP/release.info
echo "done copying file to local server"
        rm -f $host:/tmp/host_name.info
        rm -f $host:/tmp/host_release.info
        rm -f $host:/tmp/release.info
echo "done deleting .info files"
done

#Adds new information from server to main file
cat $PATH_TEMP/release.info >> servers_release.info
echo "done concat"
rm $PATH_TEMP/release.info
echo "done -rm"
touch $host:/tmp/host_name.info 

is NOT correct, should be something like

ssh $host "touch /tmp/host_name.info"

instead.

Same remark for

rm -f $host:/tmp/host_name.info 
rm -f $host:/tmp/host_release.info rm -f $host:/tmp/release.info

as well as

paste -d ";" $host:/tmp/host_name.info $host:/tmp/host_release.info >> $host:/tmp/release.info

It looks incorrect, you should use ssh instead.

Only 'scp' tolerates such $host:$absolutefilename notation

So if I understood correctly, something like this would look a lot better?

# Logs to each server using file and append the name of file with the release description into
# a single file and copies it to harp server
echo "entering for loop"

for host in `cat $PATH_TEMP/test1.txt`
    do
    echo "inside do loop"
        #logs to server and gets release information into file
    echo "before ssh"
        ssh $host "more /etc/redhat-release >> $host:/tmp/host_release.info"
    echo "done creating release file"
        #creates file and adds name of server to the file
        ssh $host "touch $host:/tmp/host_name.info"
    echo "done creating file with server name"
        ssh $host "echo $host >> /tmp/host_name.info"
    echo "done modifying server name file"
        #concatenates both files into a single file with a ';'
        ssh $host "paste -d ";" $host:/tmp/host_name.info $host:/tmp/host_release.info >> $host:/tmp/release.info"
    echo "done formatting file"
        scp -p /tmp/release.info harp:$PATH_TEMP/release.info
    echo "done copying file to local server"
        ssh $host "rm -f $host:/tmp/host_name.info"
        ssh $host "rm -f $host:/tmp/host_release.info"
        ssh $host "rm -f $host:/tmp/release.info"
    echo "done deleting .info files"
done

#Adds new information from server to main file
cat $PATH_TEMP/release.info >> servers_release.info
echo "done concat"
rm $PATH_TEMP/release.info
echo "done -rm"

No
remove '$host:' in every filename except when used in an scp

Also read the :

man ssh

and pay special attention to the options '-n' '-x' '-t' '-X' '-T'

all depend what you are trying to do and where you are willing to do
If you are on serv1 , when you want a command to be executed on another system serv2
just

ssh serv2 "command"

read the man page

# Logs to each server using file and append the name of file with the release description into
# a single file and copies it to harp server
echo "entering for loop"

for host in `cat $PATH_TEMP/test1.txt`
    do
    echo "inside do loop"
        #logs to server and gets release information into file
    echo "before ssh"
        ssh $host "more /etc/redhat-release >> /tmp/host_release.info"
    echo "done creating release file"
        #creates file and adds name of server to the file
        ssh $host "touch /tmp/host_name.info"
    echo "done creating file with server name"
        ssh $host "echo $host >> /tmp/host_name.info"
    echo "done modifying server name file"
        #concatenates both files into a single file with a ';'
        ssh $host "paste -d ";" /tmp/host_name.info /tmp/host_release.info >> /tmp/release.info"
    echo "done formatting file"
        scp -p /tmp/release.info harp:$PATH_TEMP/release.info
    echo "done copying file to local server"
        ssh $host "rm -f /tmp/host_name.info"
        ssh $host "rm -f /tmp/host_release.info"
        ssh $host "rm -f /tmp/release.info"
    echo "done deleting .info files"
done

#Adds new information from server to main file
cat $PATH_TEMP/release.info >> servers_release.info
echo "done concat"
rm $PATH_TEMP/release.info
echo "done -rm"

This is what I get on my console...

[root@harp eplc]# sh lastScript.sh
  entering for loop
  inside do loop
  before ssh ---------------> hangs in here

  Killed by signal 2. --------> I do a CTRL-C

  done creating release file
  done creating file with server name
  done modifying server name file
  paste: option requires an argument -- d
  Try `paste --help' for more information.
  lastScript.sh: line 19:  /tmp/host_name.info /tmp/host_release.info >> /tmp/release.info: No such file or directory
  done formatting file
  Host key verification failed.
  lost connection
  done copying file to local server
  done deleting .info files
  inside do loop
  before ssh
  ssh_exchange_identification: Connection closed by remote host
  done creating release file
  ssh_exchange_identification: Connection closed by remote host
  done creating file with server name
  ssh_exchange_identification: Connection closed by remote host
  done modifying server name file
  ssh_exchange_identification: Connection closed by remote host
  lastScript.sh: line 19:  /tmp/host_name.info /tmp/host_release.info >> /tmp/release.info: No such file or directory
  done formatting file
  ssh_exchange_identification: Connection closed by remote host
  done copying file to local server
  done deleting .info files
  inside do loop
  before ssh
  Killed by signal 2.

For this error...

paste: option requires an argument -- d
   Try `paste --help' for more information.

my paste command has a -d, I am thinking that the multiple "" are making noise in there?

For this error...

lastScript.sh: line 19:  /tmp/host_name.info /tmp/host_release.info >> /tmp/release.info: No such file or directory

do i need the $host:/ for this to work?

Thank you

paste -d \";\"

since the double quote are inside other double quote, you should escape them with a backslash

What are you trying to do with the following command :

ssh $host "more /etc/redhat-release >> /tmp/host_release.info"

?
do you want the file name to be append in /tmp/release.info ?? (if so, use "echo" instead of "more")
or do you want the content of the file /etc/redhat-release to be append in /tmp/release.info ?? (if so, use "cat" instead of "more")

I will go ahead and try your suggestions...
lets hope this helps...

I do want the content of the file /etc/redhat-release to be append in /tmp/release.info,
so I will use cat instaed..

Thanks

---------- Post updated at 03:08 PM ---------- Previous update was at 02:47 PM ----------

this is the script I used...

# Logs to each server using file and append the name of file with the release description into
# a single file and copies it to harp server
echo "entering for loop"

for host in `cat $PATH_TEMP/test1.txt`
    do
    echo "inside do loop"
        #logs to server and gets release information into file
    echo "before ssh"
        ssh $host "cat /etc/redhat-release >> /tmp/host_release.info"
    echo "done creating release file"
        #creates file and adds name of server to the file
        ssh $host "touch /tmp/host_name.info"
    echo "done creating file with server name"
        ssh $host "echo $host >> /tmp/host_name.info"
    echo "done modifying server name file"
        #concatenates both files into a single file with a ';'
        ssh $host "paste -d \";\" /tmp/host_name.info /tmp/host_release.info >> /tmp/release.info"
    echo "done formatting file"
        scp -p $host:/tmp/release.info harp:$PATH_TEMP/release.info
    echo "done copying file to local server"
        ssh $host "rm -f /tmp/host_name.info"
        ssh $host "rm -f /tmp/host_release.info"
        ssh $host "rm -f /tmp/release.info"
    echo "done deleting .info files"
done

#Adds new information from server to main file
cat $PATH_TEMP/release.info >> servers_release.info
echo "done concat"
rm $PATH_TEMP/release.info
echo "done -rm"

this is what I get on my shell

  [root@edsrep1 tmp]# more host_name.info
  edsrep1
  edsrep1
  edsrep1
  [root@edsrep1 tmp]# more host_release.info
  ::::::::::::::
  /etc/redhat-release
  ::::::::::::::
  Red Hat Enterprise Linux AS release 3 (Taroon Update 4)
  ::::::::::::::
  /etc/redhat-release
  ::::::::::::::
  Red Hat Enterprise Linux AS release 3 (Taroon Update 4)
  Red Hat Enterprise Linux AS release 3 (Taroon Update 4)
  [root@edsrep1 tmp]# cat /etc/redhat-release >> /tmp/host_release.info2
  [root@edsrep1 tmp]# more host_release.info2
  Red Hat Enterprise Linux AS release 3 (Taroon Update 4)
  [root@edsrep1 tmp]# more release.info
  edsrep1;::::::::::::::
  edsrep1;/etc/redhat-release
  edsrep1;::::::::::::::
  ;Red Hat Enterprise Linux AS release 3 (Taroon Update 4)
  ;::::::::::::::
  ;/etc/redhat-release
  ;::::::::::::::
  ;Red Hat Enterprise Linux AS release 3 (Taroon Update 4)
  ;Red Hat Enterprise Linux AS release 3 (Taroon Update 4)
  

It looks like its redoing the same thing 3 times for the same server. The file where all the servers are listed looks like this
test1.txt
edsrep1
edsrep2
edsrep3

so the 1st time this script is exected, this should be the content of file file copied over ssh

release.info
edsrep1;Red Hat Enterprise Linux AS release 3 (Taroon Update 4)