Unix one-liner to swap/transpose two lines in multiple text files?

后端 未结 8 1866
谎友^
谎友^ 2021-01-06 03:22

I wish to swap or transpose pairs of lines according to their line-numbers (e.g., switching the positions of lines 10 and 15) in multiple text files using a UNIX tool such a

8条回答
  •  旧巷少年郎
    2021-01-06 04:23

    This might work for you (GNU sed):

    sed -ri '10,15!b;10h;10!H;15!d;x;s/^([^\n]*)(.*\n)(.*)/\3\2\1/' f1 f2 fn
    

    This stores a range of lines in the hold space and then swaps the first and last lines following the completion of the range.

    The i flag edits each file (f1,f2 ... fn) in place.

提交回复
热议问题