×
UNIX.COM Login
Username:
Password:  
Show Password






👤


Shell Programming and Scripting

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.

Shell Programming and Scripting


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-19-2018
mohtashims mohtashims is offline
Registered User
 
Join Date: Jun 2011
Last Activity: 15 July 2018, 4:47 PM EDT
Posts: 810
Thanks: 216
Thanked 7 Times in 7 Posts
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
Sponsored Links
    #2  
Old 04-19-2018
durden_tyler's Unix or Linux Image
durden_tyler durden_tyler is offline Forum Advisor  
Registered User
 
Join Date: Apr 2009
Last Activity: 9 May 2018, 9:38 PM EDT
Posts: 2,091
Thanks: 23
Thanked 389 Times in 351 Posts


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.
Sponsored Links
    #3  
Old 04-19-2018
wbport wbport is offline
Registered User
 
Join Date: Sep 2013
Last Activity: 13 July 2018, 4:01 PM EDT
Location: Mississippi
Posts: 150
Thanks: 38
Thanked 21 Times in 21 Posts
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)
Sponsored Links
Reply

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
Replace all decimal values in a column siya@ UNIX for Dummies Questions & Answers 3 03-04-2015 08:03 AM
Rounding decimal values in a column ashu0001 Shell Programming and Scripting 3 05-18-2013 01:20 AM
How to get decimal values ? NARESH1302 Shell Programming and Scripting 3 03-14-2011 08:03 AM
print unique values of a column and sum up the corresponding values in next column amigarus Shell Programming and Scripting 6 12-21-2009 08:46 AM
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 02:29 AM.

Unix & Linux Forums Content Copyright1993-2018. All Rights Reserved.