linux中实现将上下两行文本合并为一行
1、测试数据
root@DESKTOP-1N42TVH:/home/test3# ls a.txt root@DESKTOP-1N42TVH:/home/test3# cat a.txt ## 测试数据 aaaa bbbb cccc dddd eeee ffff
2、sed实现
root@DESKTOP-1N42TVH:/home/test3# ls a.txt root@DESKTOP-1N42TVH:/home/test3# cat a.txt aaaa bbbb cccc dddd eeee ffff root@DESKTOP-1N42TVH:/home/test3# sed -n '{N; s/\n/ /p}' a.txt ## sed实现 aaaa bbbb cccc dddd eeee ffff
root@DESKTOP-1N42TVH:/home/test3# ls a.txt root@DESKTOP-1N42TVH:/home/test3# cat a.txt aaaa bbbb cccc dddd eeee ffff root@DESKTOP-1N42TVH:/home/test3# sed 'N; s/\n/ /' a.txt aaaa bbbb cccc dddd eeee ffff
3、xargs实现
root@DESKTOP-1N42TVH:/home/test3# ls a.txt root@DESKTOP-1N42TVH:/home/test3# cat a.txt aaaa bbbb cccc dddd eeee ffff root@DESKTOP-1N42TVH:/home/test3# cat a.txt | xargs -n 2 aaaa bbbb cccc dddd eeee ffff
4、awk实现
root@DESKTOP-1N42TVH:/home/test3# ls a.txt root@DESKTOP-1N42TVH:/home/test3# cat a.txt aaaa bbbb cccc dddd eeee ffff root@DESKTOP-1N42TVH:/home/test3# awk '{if(NR %2 == 0) {print $0} else {printf("%s ", $0)}}' a.txt aaaa bbbb cccc dddd eeee ffff
root@DESKTOP-1N42TVH:/home/test3# ls a.txt root@DESKTOP-1N42TVH:/home/test3# cat a.txt aaaa bbbb cccc dddd eeee ffff root@DESKTOP-1N42TVH:/home/test3# awk '{tmp = $0; getline; print tmp"\t"$0}' a.txt aaaa bbbb cccc dddd eeee ffff