2

I have this small script to test a FTP site:

#!/bin/bash wget -O /dev/null ftp://someftpsite:[email protected]/testdump300 2>&1 | \ grep '\([0-9.]\+ [M]B/s\)' >> wget300.log 

And it shows output like this:

2018-07-26 22:30:06 (22.7 MB/s) - '/dev/null' saved [104857600] 

Ok, and now I just want to have it like this:

2018-07-26 22:30:06 22.7 

Anyone who can help? I suspect I should use awk or sed?

2 Answers 2

1

Using awk:

$ wget -O /dev/null ftp://someftpsite:[email protected]/testdump300 2>&1 | \ awk '/[0-9]+ [M]B\/s/{ sub("\\(",""); print $1,$2,$3 }' >> wget300.log 

This will eliminate your need for grep as awk can also search for regex patterns. It will remove the ( before the speed and then print columns 1, 2, and 3. (date, time, and speed).

1

Here's an alternative using sed:

$ wget -O /dev/null ftp://someftpsite:[email protected]/testdump300 2>&1 | \ sed 's/(//;s/ [[:alpha:]]\+\/s.*$//' >> wget300.log 

How it works:

  • s/(//; - deletes the first parenthesis
  • s/ [[:alpha:]]\+\/s.*$// - deletes everything starting from the space + 'MB/s' to the end .*$.

Another way using perl:

$ wget -O /dev/null ftp://someftpsite:[email protected]/testdump300 2>&1 | \ perl -lne 'print "$1 $2" if /^(.*)\s\((\S+)/' >> wget300.og 

How it works:

  • anything wrapped in parenthesis in Perl will be saved, hence the $1 and $2 variables. In this case we're matching everything up to but not including the space + parenthesis in $1 and everything after the open parenthesis that's not a space \S+ in the 2nd variable, $2.

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.