0

I have a file which several lines, let's say:

server_file.txt:

[main]
server1 param1 param2 param3
server2 param1 param2 param3

I want to take the first value of last column to compose a filename within an expect script so that the filename is: server2_030417T124700, where 030417T124700 is the current date when the file was created.

I have been able to achieve the date part with these lines:

#!/usr/bin/expect -f
log_file ./log/post.log
<some_code>
system mv ./log/post.log ./log/post_[clock format [clock seconds] -format {%Y%m%dT%H%M%S}].log

But where I have "post_" I would like to have "server2" based on last line of server_file.txt

I have tried to do it by using a variable that executes the bash command tail -1 server_file.txt | awk '{print $1}' with no success and I think it is possible to do it without using a bash command.

3

Add these 3 lines of tcl code in your expect script to get the server name.

set fp [open "server_file.txt" r]
while {[gets $fp data] >= 0} {set servername [lindex [regexp -inline -all -- {\S+} $data] 0]}
close $fp
1

It has been done avoiding the tail command and using the proper syntax for awk when executed from expect script:

#!/usr/bin/expect -f
log_file ./log/post.log
set servername [exec awk {FNR>1 {print $1}} server_file.txt]
system mv ./log/post.log ./log/"$servername"_[clock format [clock seconds] -format {%Y%m%dT%H%M%S}].log

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.