captain yossarian
captain yossarian

Reputation: 457

Move text before delimiter to the end of the line

I have a file with output like this:

MIKE;123456
JOHN-HELPER;654321
SAM.SMITH;182364

I need everything before the delimiter to move to the end of the line, so it'll look like this:

123456;MIKE
654321;JOHN-HELPER
182364;SAM.SMITH

Struggling it work it out with sed... any ideas?

Upvotes: 0

Views: 1649

Answers (1)

fedorqui
fedorqui

Reputation: 290075

Like this, for example:

$ sed -r 's/([^;]*);(.*)/\2;\1/' a
123456;MIKE
654321;JOHN-HELPER
182364;SAM.SMITH

It "catches" two groups: everything before ; and then the rest. Next step is to print these blocks the other way round: \2;\1.

Or with awk:

$ awk -F";" '{print $2";"$1}' a
123456;MIKE
654321;JOHN-HELPER
182364;SAM.SMITH

It sets ; as field delimiter and then prints the fields the other way round.

Upvotes: 2

Related Questions