Sunny
Sunny

Reputation: 103

Swapping words between others words and numbers in a line using sed

I am trying to swap the words which occur between words and numbers : I have large file and lines have this order but numbers are different, but the words "WORD , AGAIN, PIC " don't change.

23  TWO-ONE2         WORD  5 AGAIN CIP D(89).           1234541120

My desired output should be :

23 TWO-ONE2 CIP D(89) WORD 5 AGAIN. 1234541120

The value near CIP " D(89) " , can be changed to F7(934) or similar, but the it is always a number in () .

I tried commands :

sed -r 's/(WORD[0-9]+) (AGAIN+) (CIP[A-Z][0-9]([A-Z][0-9])+)/\3 \1 \2/'

sed -E 's/^(WORD.+AGAIN).+(CIP \+)/\2 \1/'

I would appreciate any help !

Upvotes: 1

Views: 74

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 627103

You may use this POSIX ERE based sed:

sed -E 's/(WORD.*AGAIN)([^[:alnum:]]+)(CIP[^(]*\([0-9]+\))/\3\2\1/g' file > newfile

See the regex demo

Details

  • (WORD.*AGAIN) - Group 1 (\1): WORD, any 0+ chars, AGAIN
  • ([^[:alnum:]]+) - Group 2 (\2): one or more chars other than alphanumeric chars
  • (CIP[^(]*\([0-9]+\)) - Group 3 (\3): CIP, 0 or more chars other than (, (, 1+ digits and then ).

Upvotes: 1

Related Questions