Need to grep for decimal values only in the second column.

Hi,

I have a file in which I need to print all the lines that have decimal values in the second column.

The below prints all the decimal values from the second column but I need the complete line to be printed.

cat hello.out | sed 's/   */:/g' | cut -d : -f 2 | ggrep  -Eo "[0-9]+\.[0-9]+"

Can you please help ?

Current output:

10.877406
10.877623
10.877905

Desired Output:

18026/1:        10.877406    lseek(10, 1480, SEEK_SET)                  = 1480
18026/1:        10.877623    fstat(1, 0xFFFFFFFF7FFFE840)                       = 0
18026/1:        10.877905    _exit(0)
$
$ cat hello.out
18026/1: 10.877406 lseek(10, 1480, SEEK_SET) = 1480
18026/1: 10.877623 fstat(1, 0xFFFFFFFF7FFFE840) = 0
18026/1: 10.877905 _exit(0)
18026/1: 10877623 fstat(1, 0xFFFFFFFF7FFFE840) = 0
18026/1: 10877905 _exit(0)
18026/1: 0.877905 _exit(0)
  
$ awk 'index($2,".") > 0' hello.out
18026/1: 10.877406 lseek(10, 1480, SEEK_SET) = 1480
18026/1: 10.877623 fstat(1, 0xFFFFFFFF7FFFE840) = 0
18026/1: 10.877905 _exit(0)
18026/1: 0.877905 _exit(0)
  
$
$

That won't check for a well-formed decimal number though (which I notice you tried to do in your grep command).
Here's a more elaborate test case:

$
$
$ cat hello.out
18026/1: 10.877406 lseek(10, 1480, SEEK_SET) = 1480
18026/1: 10.877623 fstat(1, 0xFFFFFFFF7FFFE840) = 0
18026/1: 10.877905 _exit(0)
18026/1: 10877623 fstat(1, 0xFFFFFFFF7FFFE840) = 0
18026/1: 1.087.7905 _exit(0)
18026/1: 0.877905 _exit(0)
18026/1: +0.877905 _exit(0)
18026/1: -0.877905 _exit(0)
18026/1: -.877905 _exit(0)
18026/1: +123.0 _exit(0)
18026/1: #0.877905 _exit(0)
18026/1: .999905 _exit(0)
18026/1: 0.87.790a5 _exit(0)
$
$ perl -lane 'print if $F[1] =~ /^[+-]*\d*\.\d+$/' hello.out
18026/1: 10.877406 lseek(10, 1480, SEEK_SET) = 1480
18026/1: 10.877623 fstat(1, 0xFFFFFFFF7FFFE840) = 0
18026/1: 10.877905 _exit(0)
18026/1: 0.877905 _exit(0)
18026/1: +0.877905 _exit(0)
18026/1: -0.877905 _exit(0)
18026/1: -.877905 _exit(0)
18026/1: +123.0 _exit(0)
18026/1: .999905 _exit(0)
$
$
 

Still, this won't check for decimal numbers in scientific notation.

One other option:

sed -n "/ [+-]*[0-9]*\.[0-9][0-9]* / p" hello.out

It writes nine lines given Durden's last example of hello.out.

1 Like