1、测试数据
root@DESKTOP-1N42TVH:/home/test3# ls a.txt root@DESKTOP-1N42TVH:/home/test3# cat a.txt e t d u e i a d g g z j c b d e w l z c b h j h
2、形式1
root@DESKTOP-1N42TVH:/home/test3# cat a.txt e t d u e i a d g g z j c b d e w l z c b h j h root@DESKTOP-1N42TVH:/home/test3# awk '{for(i = 1; i <= NF; i++) if(i % 2 != 0) {printf("%s", $i)} else {printf("%s ", $i)} printf("\n")}' a.txt et du ei ad gg zj cb de wl zc bh jh
3、形式2
(1)
root@DESKTOP-1N42TVH:/home/test3# ls a.txt root@DESKTOP-1N42TVH:/home/test3# cat a.txt e t d u e i a d g g z j c b d e w l z c b h j h root@DESKTOP-1N42TVH:/home/test3# awk '{for(i = 1; i <= NF; i++) if(i % 2 != 0) printf("%s ", $i); printf("\n")}' a.txt > add root@DESKTOP-1N42TVH:/home/test3# awk '{for(i = 1; i <= NF; i++) if(i % 2 == 0) printf("%s ", $i); printf("\n")}' a.txt > even root@DESKTOP-1N42TVH:/home/test3# ls a.txt add even root@DESKTOP-1N42TVH:/home/test3# cat add e d e a g z c d w z b j root@DESKTOP-1N42TVH:/home/test3# cat even t u i d g j b e l c h h root@DESKTOP-1N42TVH:/home/test3# cat add even e d e a g z c d w z b j t u i d g j b e l c h h
(2)
root@DESKTOP-1N42TVH:/home/test3# ls a.txt root@DESKTOP-1N42TVH:/home/test3# cat a.txt e t d u e i a d g g z j c b d e w l z c b h j h root@DESKTOP-1N42TVH:/home/test3# for i in `seq $(expr $(head -n 1 a.txt | awk '{print NF}') / 2)`; do awk -v a=$i '{print $(2*a-1), $(2*a)}' a.txt | sed 's/ /\n/' | paste -s -d " " >> temp; done root@DESKTOP-1N42TVH:/home/test3# ls a.txt temp root@DESKTOP-1N42TVH:/home/test3# cat temp e t a d c b z c d u g g d e b h e i z j w l j h root@DESKTOP-1N42TVH:/home/test3# for i in `seq $(head -n 1 temp | awk '{print NF}')`; do cut -d " " -f $i temp | paste -s -d " " >> result; done root@DESKTOP-1N42TVH:/home/test3# ls a.txt result temp root@DESKTOP-1N42TVH:/home/test3# cat result ## 结果文件 e d e t u i a g z d g j c d w b e l z b j c h h root@DESKTOP-1N42TVH:/home/test3# cat a.txt e t d u e i a d g g z j c b d e w l z c b h j h