Home Man
Search
Today's Posts
Register

BSD, Linux, and UNIX shell scripting Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

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

Tags
shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 04-19-2018
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.

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

Can you please help ?

Current output:

Code:
10.877406
10.877623
10.877905

Desired Output:

Code:
18026/1:        10.877406    lseek(10, 1480, SEEK_SET)                  = 1480
18026/1:        10.877623    fstat(1, 0xFFFFFFFF7FFFE840)                       = 0
18026/1:        10.877905    _exit(0)


Last edited by Scrutinizer; 04-19-2018 at 01:45 PM.. Reason: quote tags -> code tags
# 2  
Old 04-19-2018
Code:
$
$ 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:

Code:
$
$
$ 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.

Last edited by durden_tyler; 04-19-2018 at 01:50 PM.. Reason: Better test case.
# 3  
Old 04-19-2018
One other option:
Code:
sed -n "/ [+-]*[0-9]*\.[0-9][0-9]* / p" hello.out

It writes nine lines given Durden's last example of hello.out.
The Following User Says Thank You to wbport For This Useful Post:
durden_tyler (04-20-2018)
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grep values from column 2 in reference of column 1 jiam912 Shell Programming and Scripting 12 05-23-2018 04:58 PM
Replace all decimal values in a column siya@ UNIX for Dummies Questions & Answers 3 03-04-2015 08:03 AM
How compare decimal values? ranabhavish Shell Programming and Scripting 3 11-13-2013 08:57 AM
Rounding decimal values in a column ashu0001 Shell Programming and Scripting 3 05-18-2013 01:20 AM
Rounding off decimal values jacobs.smith Shell Programming and Scripting 2 02-19-2013 05:55 PM
How to get decimal values ? NARESH1302 Shell Programming and Scripting 3 03-14-2011 08:03 AM
How to sum up two decimal values? mady135 Shell Programming and Scripting 7 10-04-2010 05:26 AM
Evaluating Decimal values larrys721 Shell Programming and Scripting 4 06-02-2008 08:31 PM
Converting Binary decimal coded values to Ascii Values gaur.deepti UNIX for Advanced & Expert Users 3 04-02-2008 12:33 PM


All times are GMT -4. The time now is 11:19 PM.

Unix & Linux Forums Content Copyright1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password