0
ttl=251 time=1.79 
ttl=251 time=1.38 

time=$(echo  $line | cut -d' ' -f2)
time= $(echo  $time | cut -d'=' -f2)

I can't store the decimal value into the variable time and it is throwing an error like below:

ping.sh: line 2: 1.79: command not found

sravs
  • 61
  • 3
  • Can you clarify how the `ttl=XXX time=Y.YY` data is accessed from your script? I see you reference a `$line` variable which presumably contains that data, but I don't see how it's initialized. – Aaron Nov 28 '18 at 15:53
  • am reading a log file between two time limits line by line i need to get the column time=1.79 but i want the decimal value 1.79 to do average max of time in between time limits – sravs Nov 28 '18 at 16:12
  • Questions posted should be self-documenting, and not require further discussion in comments. If something is worth putting in a comment to explain your problem, then you should edit your Q, reply to the commentor in comments with "see updated Q above". Also, a good Q will have 1. the smallest set of data to illustrate the problem, 2. the required output from that same set of data, 3. Your best attempt to solve your problem in code, 4. Current output and error messages, 5. your thoughts about why it should work but isn't working. ..... – shellter Nov 28 '18 at 16:23
  • Please read the [help] and [mcve] before posting more Qs here. Also it's worth searching for your error message, as your Q has likely been asked before (in a slightly different form) and can keep your project moving much faster, rather than waiting for an answer. OR you can improve your Q and say "I saw post X, and I thought the solution should work for me, but it didn't (or similar). ALSO, you can get faster solutions by cut/paste your code into https://shellcheck.net (be sure to include `#!/bin/bash` or your shell of choice as the first line of input. Good luck. – shellter Nov 28 '18 at 16:25

2 Answers2

1

The correct line is below:

time=$(echo  $time | cut -d'=' -f2)

There is an unwanted space, use shellcheck to troubleshoot, see below:

shellcheck /tmp/test.sh

.

In /tmp/test.sh line 2:
time= $(echo  $time | cut -d'=' -f2))
    ^-- SC1007: Remove space after = if trying to assign a value (for empty string, use var='' ... ).
       ^-- SC2091: Remove surrounding $() to avoid executing output.
       ^-- SC2116: Useless echo? Instead of 'cmd $(echo foo)', just use 'cmd foo'.

The new script would be:

line1="ttl=251 time=1.79"
line2="ttl=251 time=1.38"

time1=$(printf "%s" "${line1}" | cut -d' ' -f2)
time1=$(printf "%s" "${time1}" | cut -d'=' -f2)

If you are using bash, the following lines are more efficient:

line1="ttl=251 time=1.79"
line2="ttl=251 time=1.38"

time1="${line1##* time=}"
Jay jargot
  • 2,745
  • 1
  • 11
  • 14
  • am getting a string from line that is time=1.79 , i want to cut the time= and store the decimal value into the variable i write – sravs Nov 28 '18 at 15:50
  • 1
    @sravs Yes, and you almost had it right, except for an extra blank after `=` in the last command, as pointed out in this answer. – Benjamin W. Nov 28 '18 at 15:53
  • after removing blank it is showing this error cut: the delimiter must be a single character Try 'cut --help' for more information. – sravs Nov 28 '18 at 16:04
  • thank u so much it is working – sravs Nov 28 '18 at 16:34
0

As we know we need to get the value of time from "ttl=251 time=1.79" we can just split by the "=" character with awk:

line="ttl=251 time=1.79"
echo "${line}" | awk '{split($0,a,"="); print a[3]}' | tr -d '\n'

tr -d to grab the numerical value without \n

NOTE: This is just for the command line. In the script, you don't need the "tr -d"

In a ping command where the output is like this:

ivo@spain-nuc-03:~/Downloads/TestStackoverflow$ ping 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=3.35 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=2.50 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=2.64 ms

We could get a list with the time:

ivo@spain-nuc-03:~/Downloads/TestStackoverflow$ ping 192.168.1.1 | awk '{split($0,a,"="); print a[4]}'

2.47 ms
6.20 ms
4.85 ms
2.52 ms
1.01 ms
2.58 ms

Just remember to change the position to 4 in awk. Hope this helps!

Ivo Yordanov
  • 146
  • 1
  • 8