Christian Stewart
Christian Stewart

Reputation: 15519

Sed Regex Issue

I'm attempting to transform:

 pid 2928's current affinity list: 0-3 pid 2928's new affinity list: 0

to

2928: 0-3 --> 0

At the moment I have the following transformations that work properly:

Regex: pid Output: 2928's current affinity list: 0-3 2928's new affinity list: 0

Regex: 's current affinity list Output: 2928: 0-3 2928's new affinity list: 0

And then the final regex, which is is not working properly:

Regex: (\d+)(.)(.)(.)((?:[a-z][a-z]+))(\s+)((?:[a-z][a-z]+))(\s+)((?:[a-z][a-z]+)) Output: 2928: 0-3 2928's new affinity list: 0

As you can see, it's not applying the regex correctly. If you test this regex in an online tester, you can see that it does pick out the right area for deletion.

Full command:

echo "pid 2928's current affinity list: 0-3 pid 2928's new affinity list: 0" | sed -e "s/pid //g" -e "s/'s current affinity list//g" -e "s/(\d+)(.)(.)(.)((?:[a-z][a-z]+))(\s+)((?:[a-z][a-z]+))(\s+)((?:[a-z][a-z]+))/-->/gim"

I must not be executing the regex correctly in the third -e for sed. Does this look wrong in any way?

Upvotes: 3

Views: 177

Answers (4)

glenn jackman
glenn jackman

Reputation: 246807

The problem was that you were using perl regular expression features (\d, \s) not recognized by sed:

sed 's/pid \([0-9]\+\)'\''s current affinity list: \([0-9]\+-[0-9]\+\) pid [0-9]\+'\''s new affinity list: /\1: \2 --> /'

The awk answer is clearly easier to maintain.

Upvotes: 1

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

Using awk is more simple here:

awk -F "[ ']" '{ print $2 ": " $7 " --> " $14}'

all the line:

echo "pid 2928's current affinity list: 0-3 pid 2928's new affinity list: 0" | awk -F "[ ']" '{ print $2 ": " $7 " --> " $14}'

Upvotes: 3

arr_sea
arr_sea

Reputation: 853

Here's a quick and dirty solution to your task, though it may not answer your underlying question:

awk '{print $2" "$6" --> "$12}' | sed 's/.s/:/'

Note that this depends on the field 2, 6, and 12 (as separated by whitespace) being predictable.

Upvotes: 1

piokuc
piokuc

Reputation: 26184

In case you are interested in a non-pure-sed solution, but a simpler one, here is a hybrid sed and awk solution:

echo  "pid 2928's current affinity list: 0-3 pid 2928's new affinity list: 0"|sed "s/[a-z']//g"|awk '{print $1 ": " $3 " --> " $6}'

Upvotes: 2

Related Questions