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

后端 未结 8 1853
谎友^
谎友^ 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:17

    if, you want swap two lines, to create script "swap.sh"

    #!/bin/sh
    sed -n "1,$((${2}-1))p" "$1"
    sed -n "${3}p" "$1"
    sed -n "$((${2}+1)),$((${3}-1))p" "$1"
    sed -n "${2}p" "$1"
    sed -n "$((${3}+1)),\$p" "$1"
    

    next

    sh swap.sh infile_name 14 26 > outfile_name
    

提交回复
热议问题