linux 中实现任意两行数据的互换
1、1、3行互换
root@PC1:/home/test2# ls a.txt swap.sh root@PC1:/home/test2# cat a.txt 1 x c 2 s d 3 e t 4 d s 5 j u 6 n x root@PC1:/home/test2# cat swap.sh ## 互换 脚本 #!/bin/bash temp1=$(sed -n $1p a.txt) temp2=$(sed -n $2p a.txt) sed -e "$1c $temp2" -e "$2c $temp1" a.txt -i root@PC1:/home/test2# bash swap.sh 1 3 root@PC1:/home/test2# cat a.txt ## 交换结果 3 e t 2 s d 1 x c 4 d s 5 j u 6 n x

2、2、5行互换
root@PC1:/home/test2# ls a.txt swap.sh root@PC1:/home/test2# cat a.txt 3 e t 2 s d 1 x c 4 d s 5 j u 6 n x root@PC1:/home/test2# cat swap.sh #!/bin/bash temp1=$(sed -n $1p a.txt) temp2=$(sed -n $2p a.txt) sed -e "$1c $temp2" -e "$2c $temp1" a.txt -i root@PC1:/home/test2# bash swap.sh 2 5 root@PC1:/home/test2# cat a.txt 3 e t 5 j u 1 x c 4 d s 2 s d 6 n x


浙公网安备 33010602011771号