4
\$\begingroup\$

I've borrowed and written the following code to output the disconnect time. All works well but I'm curious as to how I could tighten/ shorten the code. If anyone feels like having some fun then I'd love to see what can be done. Be a learning lesson for me.

Excerpt of input:

ftp> !:--- FTP commands below here ---
ftp> lcd C:\Utilities\Performance_Testing\
\Utilities\Performance_Testing\: File not found 
Verbose mode On .
ftp> verbose
binary
200 Switching to Binary mode.
ftp> put "test_file_5M.bin"
200 PORT command successful.
150 Ok to send data.
226 File receive OK.
ftp: 5242880 bytes sent in Seconds Kbytes/sec.
ftp> 44.81117.00disconnect
221 Goodbye.
ftp> bye 

Code:

#Obtain UT external put value.
ut1intput=$(awk '
  NR==70 {
    for(i=1;i<=NF;i++) { 
      if($i=="ftp>") {
        sub(/disconnect/, "", $(i+1));
        print $(i+1)
      }
    }
  }' filename.txt)

utintputvalue=`echo $ut1intput | awk -F. '{print $2"."$3}'| sed  's/^..//'`

Output:

utintputvalue is 117.00
\$\endgroup\$
4
  • \$\begingroup\$ Is your first "output" actually the input in the context of this question? Could you explain what "44.81117.00" means, and why that translates to a desired output value of 1220.98? \$\endgroup\$ Commented Apr 2, 2014 at 17:55
  • \$\begingroup\$ Sorry, I C/P and pasted the incorrect output (not enough coffee). Should read 117.00. The "44.81117.00" is FTP output in seconds. I've been asked to output "after the 100th of a second". \$\endgroup\$ Commented Apr 2, 2014 at 18:18
  • \$\begingroup\$ To clarify again… 44.81117.00 means a bit over 44.81 seconds, and you care only about the fact that it was 117.00 × 10^-5 seconds more? \$\endgroup\$ Commented Apr 2, 2014 at 18:29
  • 1
    \$\begingroup\$ Should have made myself clearer. The 44.81 is the output in seconds. The 117.00 is the speed in kb. I'm grabbing the data "after the 100th of a second". for some reason the info I'm be provided has this in that odd format. \$\endgroup\$ Commented Apr 2, 2014 at 18:38

1 Answer 1

5
\$\begingroup\$

Assuming the "Output" section is stored in "filename.txt", and assuming you have GNU tools:

grep -oP '(?<=\.\d\d)\d+\.\d\d(?=disconnect)' filename.txt

That's a perl regex meaning: prececeded dot and two digits, find some digits, a dot and two digits, followed by "disconnect". grep's -o option means "output only the matched text".

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.