Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting alphanumeric values

Tags:

grep

bash

awk

I've got files in the following format

 m.dat -c16 -S32m  1.3768702014349401 s, rate:  3.2434134115834929 GB/s.
 m.dat -c16 -S64m  1.0852226612623781 s, rate:  4.115062684139847 GB/s.
 m.dat -c20 -S1m  3.8889309875667095 s, rate:  1.1483256688332133 GB/s.
 m.dat -c20 -S2m  16.622251618420705 s, rate:  0.26866151348562284 GB/s.
 m.dat -c20 -S4m  4.5505061785224825 s, rate:  0.98137637927430543 GB/s.
 m.dat -c20 -S8m  2.4563963813707232 s, rate:  1.8180124800752873 GB/s.

and I would like to extract different numeric values from them. In particular, I am after getting something similar to this:

m.dat 20 4  4.5505061785224825  0.98137637927430543

That is, I would like to extract the numeric values without characters plus the first field of every row in the file.

I can easily get the different field of every row with awk, but those values would also include -c and -S, which are not of interest.

awk '{print $1, $2, $3, $4}' file
like image 898
Manolete Avatar asked Mar 18 '26 06:03

Manolete


1 Answers

Here's a tricky bit of perl:

$ perl -lane '@fields=(@F[0], /(\d+(?:\.\d*)?|\d*\.\d+)/g); print "@fields"' file
m.dat 16 32 1.3768702014349401 3.2434134115834929
m.dat 16 64 1.0852226612623781 4.115062684139847
m.dat 20 1 3.8889309875667095 1.1483256688332133
m.dat 20 2 16.622251618420705 0.26866151348562284
m.dat 20 4 4.5505061785224825 0.98137637927430543
m.dat 20 8 2.4563963813707232 1.8180124800752873

Gotta run, explanation upon request.

like image 186
glenn jackman Avatar answered Mar 19 '26 21:03

glenn jackman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!