Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I'm puzzled here about awk, sed, etc

Tags:

bash

sed

awk

I'm trying for a while to work this out with no success so far I have a command output that I need to chew to make it suitable for further processing

The text I have is:

1/2 [3] (27/03/2012 19:32:54) word word word word 4/5

What I need is to extract only the numbers 1/2 [3] 4/5 so it will look:

1 2 3 4 5

So, basically I was trying to exclude all characters that are not digits, like "/", "[", "]", etc. I tried awk with FS, tried using regexp, but none of my tries were successful.

I would then add something to it like first:1 second:2 third:3 .... etc Please take in mind I'm talking about a file that contains a lot if lines with the same structure, but I already though about using awk to sum every column with

awk '{sum1+=$1 ; sum2+=$2 ;......etc} END {print "first:"sum1 " second:"sum2.....etc}'

But first I will need to extract only the relevant numbers, The date that is in between "( )" can be omitted completely but they are numbers too, so filtering merely by digits won't be enough as it will match them too

Hope you can help me out Thanks in advance!

like image 639
TuxSax Avatar asked Apr 04 '12 08:04

TuxSax


Video Answer


2 Answers

This: sed -r 's/[(][^)]*[)]/ /g; s/[^0-9]+/ /g' should work. It makes two passes, removing parenthesized expressions first and then replacing all runs of non-digits with single spaces.

like image 64
Michał Kosmulski Avatar answered Sep 20 '22 16:09

Michał Kosmulski


You can do something like sed -e 's/(.*)//' -e 's/[^0-9]/ /g'. It deletes everything inside the round brackets, than substitutes all non-digit characters with a space. To get rid of extra spaces you can feed it to column -t:

$ echo '1/2 [3] (27/03/2012 19:32:54) word word word word 4/5' | sed -e 's/(.*)//' -e 's/[^0-9]/ /g' | column -t
1  2  3  4  5
like image 37
Lev Levitsky Avatar answered Sep 19 '22 16:09

Lev Levitsky