Here is the output given by speedtest-cli: (personal information redacted with test data)
Retrieving speedtest.net configuration...
Retrieving speedtest.net server list...
Testing from ------ (xxx.xxx.xxx.xxx)...
Selecting best server based on latency...
Hosted by ------- (------, --) [15.00 km]: 50.00 ms
Testing download speed........................................
Download: 60.00 Mbit/s
Testing upload speed..................................................
Upload: 10.00 Mbit/s
Where I want the output to be a comma separated line of ping, dl, ul:
50.00, 60.00, 10.00
I have been working on a solution and have come up with this:
speedtest-cli | sed -n "5p;7p;9p" | grep -oE "[[:digit:]]{1,}" | tr '\n' ,
Which outputs:
15,00,50,00,60,00,10,00,
Which is close to what I want. Except that it is including the distance (15.00km) from the 5th line and splitting based on . as well. Is there a better way to do this using awk or something similar?
Using awk you can do:
speedtest-cli | awk -v ORS=', ' '/^(Hosted|Download|Upload)/{print $(NF-1)}'
50.00, 60.00, 10.00,
To use newline instead of trailing , use:
speedtest-cli | awk -v ORS=', ' '/^(Hosted |Download:)/{print $(NF-1)}
/^Upload:/{printf "%s%s", $(NF-1), RS}'
50.00, 60.00, 10.00
If your grep supports PCRE (-P), you can do:
% grep -Po '^(Hosted|Download|Upload).*:\K [^ ]+' file.txt | tr '\n' ','; echo
50.00, 60.00, 10.00,
So:
speedtest-cli | grep -Po '^(Hosted|Download|Upload).*:\K [^ ]+' | tr '\n' ','; echo
Related
I want to Filter all content after match with the content and bring the first value after the "."
I have an output something like this:
Output:
product: 13.6.0.35_0
More specifically, I need only the first two digits and the first digit after the dot, remembering that we should not cling to the values in the issue, but rather on the method of filtering the content.
Expected:
13.6
I tried something like:
echo "product: 13.6.0.35_0" | grep -ow '\w*13\w*'
If you need to use grep with the current logic, you can use
echo "product: 13.6.0.35_0" | grep -ow '13\.[0-9]*' | head -1
where 13\.[0-9]* matches 13, . and zero or more digits (as whole word due to w option) and head -1 gets the first match.
You may also use sed or awk:
sed -En 's/.* ([0-9]+\.[0-9]+).*/\1/p' <<< "product: 13.6.0.35_0"
awk -F'[[:space:].]' '{print $2"."$3}' <<< "product: 13.6.0.35_0"
See the online demo.
The sed command matches any text up to space, then matches the space and captures the two subsequent dot-separated numbers into Group 1 (\1) and then the rest of the line is matched and replaced with Group 1 value that is printed (as the default line output is suppressed with -n).
In the awk command, the field separator is set to whitespace and . with -F'[[:space:].]' and the {print $2"."$3} part prints the second and third field values joined with a ..
A pure shell solution using the builtin read , Parameter Expansion and curly braces for command groupings.
echo "product: 13.6.0.35_0" | { read -r _ value; echo "${value%.*.*}" ; }
You can also use cut:
echo 'product: 13.6.0.35_0' | cut -d ' ' -f2 | cut -d '.' -f1-2
13.6
I reached the expected output, it's simple but it works:
var=$(echo "product: 13.6.0.35_0" | grep -Eo '[[:digit:]]+' | sed -n 1,2p)
echo ${var} | sed 's/ /./g'
I'm trying to get grep/sed out the following output: "name":"test_backup_1" from the below response
{"backups":[{"name":"test_backup_1","status":"CORRUPTED","creationTime":"2019-11-08T15:03:49.460","id":"test_backup_1"}]}
I have been trying variations of the following grep -Eo 'name:"\w+\"' but no joy.
I'm not sure if it would be easier to achieve this using grep or sed?
The way I am running this is curling a response from the server and saving it to a local variable, then echo out the variable and pipe grep/sed
example of what I am running
echo ${view_backup} | grep -Eo '"name":"\w+\"'
Referencing #sundeep answer
grep -Eo '"name":"[^"]+"'
resulted in the expected output
Make sure to transform the file to one line before grep
and pipe from your curl
echo `curl --silent https://someurl | tr -d '\n' | grep -oP "(?<=name\":\")[^\"]+"`
will return
test_backup_1
If you want more variables you can chain the -oP grep like in this example where I get some data on a danish license plate (bt419329)
curl --silent https://www.tjekbil.dk/api/v2/nummerplade/bt41932 | grep -oP -m 1 "(?<=\"RegNr\":\")[^\"]+|(?<=\"MaerkeTypeNavn\":\")[^\"]+|(?<=\"MaksimumHastighed\":)[^,]+"| tr '\n' ' '
returns
BT41932 SKODA 218
I have this string;
4.0K /Server/mysql/backup/backup_mysql_ltr_20151006-131057.tar.gz
and I need only the 4.0K returned, however obviously, this number could be anything. I believe there is a tab between the K and the /Server
Any ideas how it could be done. It'll be used in a terminal/bash command on Mac OS X and Ubuntu.
<command that produces your output> | cut -f 1
And if that is not a tab between the K and the /Server, then
<command that produces your output> | cut -f 1 -d' '
One approach is to just use awk:
sz=$(echo "$string" | awk '{print $1}')
as per the following transcript:
pax> string="4.0K /Server/blah_blah_blah.tar.gz"
pax> sz=$(echo "$string" | awk '{print $1}')
pax> echo $sz
4.0K
There are many other approaches using cut, sed, grep -o and so on(1) but I usually use awk because:
it lends itself naturally to white-space separated fields; and
it tends to allow for more powerful programming than the others.
(1) Such as:
sz=$(echo "$string" | cut -f1)
sz=$(echo "$string" | sed 's/\t.*$//')
sz=$(echo "$string" | grep -o $'^[^\t]*')
and so on...
s="4.0K /Server/mysql/backup/backup_mysql_ltr_20151006-131057.tar.gz"
x="${s// *}"
echo "$x"
Output:
4.0K
I have strings like the following: blabla a13724bla-bla244 35%
Notice that there is always a space before the percentage. I would like to extract the percentage number (so, without the %) from these strings using the Linux shell.
Assuming you have GNU grep:
$ grep -oP '\d+(?=%)' <<< "blabla a13724bla-bla244 35%"
35
Using sed:
echo blabla a13724bla-bla244 35% | sed 's/.*[ \t][ \t]*\([0-9][0-9]*\)%.*/\1/'
If you expect to have multiple percentages in a line then:
echo blabla 20% a13724bla-bla244 35% | \
sed -e 's/[^%0-9 ]*//g;s/ */\n/g' | sed -n '/%/p'
You can try this
echo "blabla a13724bla-bla244 35%" | cut -d' ' -f3 | sed 's/\%//g'
NOTE: Assumption is the input is always in this format and percentage is 3rd token separated by space.
You may try this regular expression:
/\s(\d+%)/
Use this regular expression:
\s(\d{1,3})%
If you need it in shell, you can use sed or this perl one-liner:
echo "blah 35%" | perl -pe "s/.*\s(\d{1,3})%/\1/g"
35
If you always have a number of continuous columns maybe you should try with awk instead of a regular expresion.
cat file.txt |awk '{print $3}' |cut -d "%" -f 1
With this code you obtain the third column.
I'm trying to parse various info from log files, some of which is placed within square brackets. For example:
Tue, 06 Nov 2007 10:04:11 INFO processor:receive: [someuserid], [somemessage] msgtype=[T]
What's an elegant way to grab 'someuserid' from these lines, using sed, awk, or other unix utility?
cut use it like this: cut -f2 -d[ | cut -f1 -d]
bart#hal9k:~> YOURTEXT="Tue, 06 Nov 2007 10:04:11 INFO processor:receive: [someuserid], [somemessage] msgtype=[T]"
bart#hal9k:~> SOMEID=`echo $YOURTEXT | cut -f2 -d[ | cut -f1 -d]`
bart#hal9k:~> echo $SOMEID
someuserid
If you want to do something with all the bracketed fields, I'd use Perl:
perl -lne '
my #fields = /\[(.*?)\]/g;
# do something with #fields, like:
print join(":", #fields);
' logfile ...
using bash shell
while read -r line
do
case "$line" in
*processor*receive* )
t=${line#*[}
echo ${t%%]*}
;;
esac
done < "file"
sed -n '/INFO/{s/.[^[]*\[//;s/\].*//p}' file
Using AWK:
cat file | awk -F[\]\[] '{print $2}'
I have found that multiple delimiters do not work in some older versions of AWK. If it doesn't, you can use two awks:
cat file | awk -F[ '{print $2}' | awk -F] '{print $1}'