[Solved] How do i deal with values on multiple lines?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [Solved] How do i deal with values on multiple lines?
# 1  
Old 04-11-2013
[Solved] How do i deal with values on multiple lines?

Hi,

I have a file which has the contents:
Code:
sh-4.2# pwd
/tmp
sh-4.2# cat servernfiles
server1 /var/tmp/file
server2 /var/tmp/file1

I want to manage each line one after the other. I have this basic script :
Code:
#!/bin/sh

HOST=`cat /tmp/servernfiles | awk '{print $1}'`
CMD=`cat /tmp/servernfiles | awk '{print $2}'`

for i in `cat /tmp/servernfiles`
do
        echo $HOST,$CMD
done

When i run the script, i was expecting to get the output of each line (i.e "server1 /var/tmp/file" followed by the other line "server2 /var/tmp/file1" ) but get this:
Code:
sh-4.2# ./servern
server1 server2,/var/tmp/file /var/tmp/file1
server1 server2,/var/tmp/file /var/tmp/file1
server1 server2,/var/tmp/file /var/tmp/file1
server1 server2,/var/tmp/file /var/tmp/file1

Can anyone please explain why this is or how i can output of each line individually?

To put this into context, im going to develop my script so it can recursively log onto each host and output the /var/tmp/file - but this bit i can do once i can figure out why im getting this first problem

Thanks!

Last edited by Franklin52; 04-12-2013 at 03:06 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 04-11-2013
Too much code is a newbies added load. "cat x|y" is "y<x". You go through the file three times. First time puts all field 1 in HOST, linefeed separated. Nest puts all field 2 in CMD linefeed separated.
Code:
$ echo '1
2
3' >123
$ z=`cat 123` ;echo "$z"|od -bc
0000000   1  \n   2  \n   3  \n
        061 012 062 012 063 012
0000006
$

Finally for every word in your file, you echo out both those lists. Good thing there are not 110 pairs in there, we'd run out of bits.

Try this:
Code:
while read HOST CMD
do
 ......
done </tmp/servernfiles

The inside is up to you.
This User Gave Thanks to DGPickett For This Post:
# 3  
Old 04-11-2013
Here is a correct way to do it:
Code:
$ cat servernfiles
server1 /var/tmp/file
server2 /var/tmp/file1

$ cat temp.sh
while read HOST CMD; do
  echo $HOST, $CMD
done < servernfiles

$ ./temp.sh
server1, /var/tmp/file
server2, /var/tmp/file1

# 4  
Old 04-12-2013
thanks for your help guys

---------- Post updated at 09:30 AM ---------- Previous update was at 07:54 AM ----------

Thank you guys for your help.


Got another question. I have added my ssh command into the loop:

Code:
$ cat servernfiles
server1 /var/tmp/file
server2 /var/tmp/file1

$ cat temp.sh
while read HOST CMD; do
echo $HOST, $CMD
ssh -q $host cat $CMD
done < servernfiles

However the loop exits after cat'ing file on the first host. If i want the loop to go through each line in servernfiles i think i'd need another loop. Can you give any pointers please ?

Last edited by zaxxon; 04-12-2013 at 06:00 AM..
# 5  
Old 04-12-2013
Yes, ssh disrupts the loop for some reason. Try this shell script:
Code:
rm -f batch
while read HOST CMD; do
  echo $HOST, $CMD
  echo "ssh $HOST cat $CMD" >> batch
done < servernfiles
chmod 755 batch
./batch


Last edited by hanson44; 04-12-2013 at 02:48 PM.. Reason: Make total mistake
This User Gave Thanks to hanson44 For This Post:
# 6  
Old 04-12-2013
Quote:
Originally Posted by hanson44
Code:
ssh -q $host cat $CMD

Was that a typo or what you intended? Seems like you cannot just jam cat command into the middle of another command like that. Or maybe I'm missing something.
It's not a commandline thing, it's an ssh thing. ssh will indeed accept commands like that, and run that command remotely instead of logging into a prompt.
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 04-12-2013
Quote:
Originally Posted by hanson44
Yes, ssh disrupts the loop for some reason. Try this shell script:
Code:
rm -f batch
while read HOST CMD; do
  echo $HOST, $CMD
  echo "ssh $HOST cat $CMD" >> batch
done < servernfiles
chmod 755 batch
./batch

You can just use ssh's -n option or redirect its stdin to /dev/null.

Regards,
Alister
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing multiple lines from input file, if multiple lines match a pattern.

GM, I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed. I am assuming that sed, awk or even perl could do what I need. I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Discussion started by: jxfish2
7 Replies

2. Shell Programming and Scripting

Reading multiple values from multiple lines and columns and setting them to unique variables.

Hello, I would like to ask for help with csh script. An example of an input in .txt file is below, the number of lines varies from file to file and I have 2 or 3 columns with values. I would like to read all the values (probably one by one) and set them to independent unique variables that... (7 Replies)
Discussion started by: FMMOLA
7 Replies

3. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

4. Shell Programming and Scripting

[Solved] Counting The Number of Lines Between Values with Multiple Variables

Hey everyone, I have a bunch of lines with values in field 4 that I am interested in. If these values are between 1 and 3 I want it to count all these values to all be counted together and then have the computer print out LOW and the number of lines with those values in between 1 and 3,... (2 Replies)
Discussion started by: VagabondGold
2 Replies

5. Shell Programming and Scripting

[SOLVED] UNIX FOR loop to read a variable with multiple values

Hi, I have a variable which stores file names as a result of find command. I need to delete all these files one by one, i.e. by a loop. Can anyone tell me how can it be done? The variable f2d has the file names like these abc.txt bcd.txt fff.txt gef.txt Now I have used a loop as... (12 Replies)
Discussion started by: jhilmil
12 Replies

6. UNIX for Dummies Questions & Answers

[SOLVED] remove lines that have duplicate values in column two

Hi, I've got a file that I'd like to uniquely sort based on column 2 (values in column 2 begin with "comp"). I tried sort -t -nuk2,3 file.txtBut got: sort: multi-character tab `-nuk2,3' "man sort" did not help me out Any pointers? Input: Output: (5 Replies)
Discussion started by: pathunkathunk
5 Replies

7. Shell Programming and Scripting

how to Insert values in multiple lines(records) within a pipe delimited text file in specific cols

this is Korn shell unix. The scenario is I have a pipe delimited text file which needs to be customized. say for example,I have a pipe delimited text file with 15 columns(| delimited) and 200 rows. currently the 11th and 12th column has null values for all the records(there are other null columns... (4 Replies)
Discussion started by: vasan2815
4 Replies

8. Shell Programming and Scripting

BASH: extracting values from multiple lines after a match

Hi there, I have the following output, # raidctl -l RAID Volume RAID RAID Disk Volume Type Status Disk Status ------------------------------------------------------ c0t1d0 IM OK c0t1d0 OK ... (4 Replies)
Discussion started by: rethink
4 Replies

9. Shell Programming and Scripting

get lines with multiple values at a position

Hi, I have a file like below: ABC,1001,DEFG,40000 AVD,2002,FGRG,3000 DBF,2002,HDGD,3454 GDF,4564,GRTR,5656 GDF,4659,GGTD,10002 .... ..... I have to get all the lines which contains 2002 and 4659 at the second position. Please help. The output file will be like: ... (9 Replies)
Discussion started by: deepakgang
9 Replies
Login or Register to Ask a Question