简体   繁体   中英

Grepping Numbers With Decimal Places

I'm trying to figure out how to grep a time stamp read from a file that consists of a number with a decimal, but I can't seem to get the output I'm looking for, any help would be great! Thanks in advance.

Example:

[Time since reference or first frame: 691.933275000 seconds]
[Time since reference or first frame: 691.953127000 seconds]
[Time since reference or first frame: 691.992275000 seconds]
[Time since reference or first frame: 691.997590000 seconds]

The result should be:

691.933275000
691.953127000
691.992275000
691.997590000

What I've Tried:

grep '[0-9]\.[0-9]' tmp.txt
grep -Eo '[0-9]\.[0-9]' tmp.txt
grep -Eo '([0-9]{1,10}[\.]){1,10}[0-9]' tmp.txt

您可以使用awk拆分文件以检索具有十进制数字的那些字段

cat file_name | awk '{print $7}'

Here is how it could be solved using Perl:

perl -lane 'for (@F) {print if /^\d+\.?\d+$/}' file

These command-line options are used:

  • -n loop around every line of the input file, do not automatically print the line

  • -l removes newlines before processing, and adds them back in afterwards

  • -a autosplit mode – split input lines into the @F array. Defaults to splitting on whitespace

  • -e execute the perl code

The perl code splits each input line into the @F array, then loops over every field and decides whether or not to print it.

The regular expression /^\\d+\\.?\\d+$/ is used on each whitespace-delimited word

  • ^ starts with

  • \\d+ one or more numerals

  • \\.? zero or one .

  • \\d+ one or more numerals

  • $ end

$ cut -d' ' -f7 file
691.933275000
691.953127000
691.992275000
691.997590000

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM