Using SED with EXPECT to trim a string from buffer
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Using SED with EXPECT to trim a string from buffer
Hi,
I am currently attempting to trim a string using SED within an Expect script.
I am looking to remove either all but the first line or everything after a specific word.
The string is:
Code:
Resistance: (T-R) 999999999 (T-S) 2108967 (R-S) 2108967 Ohm
999999999 Indicates out of range result
operation completed.
The code I am using is:
Code:
#set the variable and dump the buffer contents in it
set res_var $expect_out(buffer)
exec cat $res_var | sed -e '1!d' > $res_var
send_user $res_var
I have tried encapsulating the 1!d in "'s, {'s and 's and nothing trims the string.
The end result I am trying to achieve is to have everything after 'Ohm' removed thus presenting one single line to the user. I know the SED coding is correct in that it will run from command line on a test file, it's the integration into Expect that's getting me.
There must be something you are not telling us because what you describe will not work on a normal Unix/Linux machine.
The reason being that the shell opens stdout before executing commands. I.e (in short):
1 - the shell sees > test.txt and opens a new file called test.txt (empty),
2 - now the cat/sed commans is executed, which uses an empty test.txt as input.
This is default behaviour.
Which Linux distro or Unix flavor are you using and which shell?
There must be something you are not telling us because what you describe will not work on a normal Unix/Linux machine.
Thanks for the explanation. I am not deliberately hiding information so I apologise for any missing information. It is configured as a network 'jump host' if that makes any difference?
I am using GNU Bash v2.05a on FreeBSD 4.8 release P13
It has always worked on the test machine but as soon as I invoked the commands within the 'spawned session' they failed with various errors. It has just dawned on me (after hours of tinkering) why, the spawned session is to an Actelis DSLAM which doesn't have an install of SED or AWK or anything I can use to manipulate the string / file.
I'm sorry if I have wasted your time.
I now figure I need to 'quit' the spawned telnet session, drop back into the unix host and SED/AWK the file. The problem is, the spawned session is killed:
Code:
send: sending "quit\r" to { exp4 }
quit
Connection closed by foreign host.
expect: read eof
expect: set expect_out(spawn_id) "exp4"
Which is effectively ending the script and not allowing further commands to be run as it expects the to be sent to exp4, the spawned session.
Do you have any pointers on how to reference the spawning (parent) session once I have killed the spawned (child) session?
Ok, for completeness and to assist anyone who is having a similar issue I have managed to 'work around' the issue by using Tcl based commands so the work is being done by expect rather than passing it to the spawned processes shell:
Create (and open) a test file, dump the contents of the buffer in there:
Code:
set tmpfile "tmp[pid].txt"
set output [open $tmpfile "w"]
set outcome $expect_out(buffer)
puts $output $outcome
close $output
Now re-open the file for writing and read the first line into a variable:
Code:
set fp [open $tmpfile "r"]
set file_data [gets $fp]
close $fp
exec rm $tmpfile
This will then allow you to output the first line of the buffer using:
Code:
send_user "$file_data"
Now I know this isn't the cleanest way of doing this, using a file and having to open it twice isn't ideal but I am trying to refine it down to create, write, read and delete in one strike.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.