how to print off results

print from an ip_list file containing 300 ip's

the directory of the results is /var/tmp/1.1.1.1

the 1.1.1.1 will change according to the /tmp/ip_list file i.e

1.1.1.1
2.2.2.2
3.3.3.3

I need the results from /var/tmp/1.1.1.1 once done the script goes to the next ip address in /tmp/ip_list line by line

here's my attempt:

while read ip
do
  cat /var/tmp/1.1.1.1 / $ip
done < ip_list

If it's the 1.1.1.1 that's being replaced, don't put 1.1.1.1 in there.

Also, don't put spaces, that will seperate it into different filenames as far as the shell's concerned.

while read ip
do
  cat /var/tmp/$ip
done < ip_list
1 Like

it seems to be printing stuff all over the place?

another requirement say if in /var/tmp/ there is only one particular line im interested in i.e the second to last line and i want this printed off then move onto the next ip address in /tmp/ip_list

how about:

while read ip
do
  printf "/var/tmp/%s: " $ip
  tail -2 /var/tmp/ip_list | head -1
done < /tmp/ip_list
1 Like

forgot to mention that the file name is test.log

/var/tmp/1.1.1.1/test.log

---------- Post updated at 01:29 AM ---------- Previous update was at 01:19 AM ----------

/var/tmp/1.1.1.1:/test.log tail: cannot open input

oops sorry typo in the script try this:

while read ip
do
  printf "/var/tmp/%s/test.log: " $ip
  tail -2 /var/tmp/$ip/test.log | head -1
done < /tmp/ip_list
1 Like

ok works thanks for the pointers.

one thing I don't understand is $ip variable is used after printf statement then in tail statement after /var/tmp/$ip don't understand this bit

Can't explain why tail if failing to open the file.

You could try:

tail -2 < /var/tmp/$ip/test.log | head -1

or

cat  /var/tmp/$ip/test.log | tail -2 | head -1
1 Like

no it was me being stupid, see my updated question on this...also

from the output I need not print off the path i.e output is

/var/tmp/1.1.1.1/test.log: HELLO 1.1.1.1: +6485s: output  failed

just need

HELLO 1.1.1.1: +6485s: output  failed

---------- Post updated at 03:25 AM ---------- Previous update was at 03:07 AM ----------

figured it, removed first printf statement

while read ip
do
  tail -2 /var/tmp/$ip/test.log | head -1
done < /tmp/ip_list

can you advise on my question on page 1 of this thread please

In a earlier post you said that things were printing all over the place. The printf was just so you could see what file each line was comming from.

printf outputs the pathname of the file with ip address slotted in at the %s position eg /var/tmp/1.1.1.1/test.log: , as no CR is on the end of this line the output of head+tail appends on the end.

1 Like

ok thanks.

few q's

how does %s & $ip know that ip_list entry should be output here? what's the correlation sorry if that sounds dumb.

also

tail -2 is second to last line
with | head -1 are we saying pipe the second to last line with 1 line only ?

The "while read ip" statment populates the ip environment variable from the file 1 line at a time. and then executes all code to the done statement.

printf has the format of:

printf format_string arg1 arg2 arg3 ...

Here is a couple of examples - (check man printf for more):

printf "%10s%6.2f\n" test 12
      test 12.00
printf "Name: %s Age:%d\n" "John Jones" 25
Name: John Jones Age:25

So:
%10s formats first argument (test) as a 10 wide right justified string (Use %-10 for left justified)
%6.2f formats 2nd argument (12) as 6 (full stop and 2 digits count in width too) wide float with 2 decimal places
%s first argument (John Jones) as a variable width string
%d 2nd argument (25) as variable width integer.