Sunday 15 February 2015

bash - How to use RegEx to match an integer before or after a specific character in Unix sed command? -



bash - How to use RegEx to match an integer before or after a specific character in Unix sed command? -

how utilize regex match integer before or after specific character in unix sed command?

suppose have sed-numbers file:

cat sed-numbers (1, 2) (4, 5) (2, 3)

now can utilize sed switch (x, y) (y, x) don't know how select first integer without select character(, so, have add together ( character in front end of pattern \2 accomplish task:

sed 's/^(\([0-9]*\), \([0-9]*\)/(\2, \1/' sed-numbers (2, 1) (5, 4) (3, 2)

is there method can switch without add together ( in switch pattern? in other words, can utilize regex select first integer , has right after ( instead of select (+integer?

additional question: if need switch position if first number integer , info format is:

(11, 2) (0.2, 5) (2.1, 3) (4, 2.1)

if input in exact format mentioned in question utilize below sed commnad,

$ sed 's/\([0-9]*\), \([0-9]*\)/\2, \1/' file (2, 1) (5, 4) (3, 2)

you through perl using lookbehind , lookahead,

$ perl -pe 's/(?<=\()(\d+), (\d+)(?=\))/\2, \1/g' file (2, 1) (5, 4) (3, 2)

update

this work updated question.

$ perl -pe 's/(?<=\()(\d+), (\d+(?:\.\d+)?)(?=\))/\2, \1/g' file (2, 11) (0.2, 5) (2.1, 3) (2.1, 4)

regex bash unix sed

No comments:

Post a Comment