Visit Our UNIX and Linux User Community


Adding extra word from file1 to file2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding extra word from file1 to file2
# 8  
Old 06-08-2011
Did you test your script ?
What error message do you get ?

Code:
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)?
# 9  
Old 06-09-2011
thanks for the replies...
first of all, do I need
Code:
$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.
Code:
# 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"

# 10  
Old 06-09-2011
Code:
touch $host:/tmp/host_name.info

is NOT correct, should be something like

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

instead.

Same remark for
Code:
rm -f $host:/tmp/host_name.info 
rm -f $host:/tmp/host_release.info rm -f $host:/tmp/release.info

as well as
Code:
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
# 11  
Old 06-09-2011
So if I understood correctly, something like this would look a lot better?

Code:
# 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"

# 12  
Old 06-09-2011
No
remove '$host:' in every filename except when used in an scp

Also read the :
Code:
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
Code:
ssh serv2 "command"

read the man page

Last edited by ctsgnb; 06-09-2011 at 12:34 PM..
# 13  
Old 06-09-2011
Code:
# 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...
Code:
[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...
Code:
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...
Code:
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
# 14  
Old 06-09-2011
Code:
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 :
Code:
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")

Last edited by ctsgnb; 06-09-2011 at 03:24 PM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #693
Difficulty: Medium
The rights to Unixware, as well as OpenServer, were acquired by UnXis in 2011.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to search field2 in file2 using range of fields file1 and using match to another field in file1

I am trying to use awk to find all the $2 values in file2 which is ~30MB and tab-delimited, that are between $2 and $3 in file1 which is ~2GB and tab-delimited. I have just found out that I need to use $1 and $2 and $3 from file1 and $1 and $2of file2 must match $1 of file1 and be in the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. UNIX for Dummies Questions & Answers

Compare file1 and file2, print matching lines in same order as file1

I want to print only the lines in file2 that match file1, in the same order as they appear in file 1 file1 file2 desired output: I'm getting the lines to match awk 'FNR==NR {a++}; FNR!=NR && a' file1 file2 but they are in sorted order, which is not what I want: Can anyone... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

3. Shell Programming and Scripting

If file1 and file2 exist then

HI, I would like a little help on writing a if statement. What i have so far is: #!/bin/bash FILE1=path/to/file1 FILE2=path/to/file2 echo ${FILE1} ${FILE2} if ] then echo file1 and file2 not found else echo FILE ok fi (6 Replies)
Discussion started by: techy1
6 Replies

4. Shell Programming and Scripting

look for line from FILE1 at FILE2

Hi guys! I'm trying to write something to find each line of file1 into file2, if line is found return YES, if not found return NO. The result can be written to a new file. Can you please help me out? FILE1 INPUT: WATER CAR SNAKE (in reality this file has about 600 lines each with a... (2 Replies)
Discussion started by: demmel
2 Replies

5. UNIX for Dummies Questions & Answers

if matching strings in file1 and file2, add column from file1 to file2

I have very limited coding skills but I'm wondering if someone could help me with this. There are many threads about matching strings in two files, but I have no idea how to add a column from one file to another based on a matching string. I'm looking to match column1 in file1 to the number... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

6. Shell Programming and Scripting

grep -f file1 file2

Hi I started to learn bash a week ago. I need filter the strings from the last column of a "file2" that match with a column from an other "file1" file1: chr10100036394-100038350AK077761 chr10100041065-100046547AK032226 chr10100041065-100046547AK016270 chr10100041065-100046547AK078231 ...... (6 Replies)
Discussion started by: geparada88
6 Replies

7. Shell Programming and Scripting

file1 newer then file2

Hello, I am new to shell scripting and i need to create a script with the following directions and I can not figure it out. Create a shell script called newest.bash that takes two filenames as input arguments ($1 and $2) and prints out the name of the newest file (i.e. the file with the... (1 Reply)
Discussion started by: mandylynn78
1 Replies

8. Shell Programming and Scripting

Read each word from File1 and search each file in file2

file1: has all words to be searched. 100007 200999 299997 File2: has all file names to be searched. C:\search1.txt C:\search2.txt C:\search3.txt C:\search4.txt Outfile: should have all found lines. Logic: Read each word in file1 and search each file in the list of File2; if the... (8 Replies)
Discussion started by: clem2610
8 Replies

9. Shell Programming and Scripting

grep -f file1 file2

Wat does this command do? fileA is a subset of fileB..now, i need to find the lines in fileB that are not in fileA...i.e fileA - fileB. diff fileA fileB gives the ouput but the format looks no good.... I just need the contents alone not the line num etc. (7 Replies)
Discussion started by: vijay_0209
7 Replies

10. Shell Programming and Scripting

match value from file1 in file2

Hi, i've two files (file1, file2) i want to take value (in column1) and search in file2 if the they match print the value from file2. this is what i have so far. awk 'FILENAME=="file1"{ arr=$1 } FILENAME=="file2" {print $0} ' file1 file2 (2 Replies)
Discussion started by: myguess21
2 Replies

Featured Tech Videos