Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove pattern in txt using awk or sed

Tags:

sed

awk

I am trying to remove a pattern from some text. I mean:

from [1426467605000,19.44] to 19.44

Here is my input text file:

[1426467605000,19.44],[1426467965000,19.44],[1426468325000,19.38],[1426468685000,19.38],[1426469045000,19.38],[1426469405000,19.38],[1426469764000,19.38],[1426470124000,19.38],[1426470484000,19.38],[1426470845000,19.31],[1426471205000,19.31],[1426471565000,19.31],[1426471925000,19.31],[1426472285000,19.31],[1426472645000,19.31],[1426473005000,19.31],[1426473365000,19.31],[1426473725000,19.31],[1426474085000,19.31],[1426474445000,19.25],[1426474805000,19.25],[1426475164000,19.25],[1426475524000,19.25],[1426475884000,19.55],[1426476245000,19.25],[1426476605000,19.25],[1426476965000,19.25],[1426477325000,19.25],[1426477685000,19.19],[1426478045000,19.19],[1426478405000,19.19],[1426478764000,19.19],[1426479124000,19.19],[1426479484000,19.19],[1426479844000,19.19],[1426480204000,19.13],[1426480564000,19.13],[1426480924000,19.19],[1426481284000,19.19],[1426481644000,19.19],[1426482005000,19.19],[1426482365000,19.19],[1426482725000,19.19],

Here is my desired output:

19.44
19.44
19.38
19.38
19.38
etc.
like image 906
Marek Bednarz Avatar asked Apr 15 '26 00:04

Marek Bednarz


2 Answers

this grep line should do it:

grep -oP '[^,]*(?=])'

in short, this line extracts text between , and ], which are the things you want.

like image 199
Kent Avatar answered Apr 17 '26 07:04

Kent


Use grep for that:

grep -oE '[0-9]+\.[0-9]+' file

The pattern searches for one or more numbers followed by a dot and again one or more numbers.

-o makes grep output the match only and the not the whole line where the match appears. -E allows us to use posix extended regular expression which saves us to escape the +.


An alternative would be to use awk like this:

awk -F, '{print $2}' RS='\\[|\\],|\\],\\[' file

This command performs a more semantic analysis, it returns the second value from a record. It separates records by [ (start of line) or ], or ],[. It prints the second field of a record where fields are delimited by ,.

If you want to allow that the last record of a line is closed without a , at the end you can simply modify the pattern to:

awk -F, '{print $2}' RS='\\[|\\],?|\\],\\[' file

which makes the comma at the end of the record separator optional.

like image 31
hek2mgl Avatar answered Apr 17 '26 09:04

hek2mgl



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!