linux系統中如何把一列數據轉換為一行數據


 

1、測試數據,測試1

[root@centos7 test2]# seq -f %02g 10 > a.txt [root@centos7 test2]# ls a.txt [root@centos7 test2]# cat a.txt 01
02
03
04
05
06
07
08
09
10

 

2、測試2

[root@centos7 test2]# cat a.txt 01
02
03
04
05
06
07
08
09
10 [root@centos7 test2]# cat a.txt | xargs 01 02 03 04 05 06 07 08 09 10

 

3、測試3

[root@centos7 test2]# cat a.txt 01
02
03
04
05
06
07
08
09
10 [root@centos7 test2]# cat a.txt | tr "\n" " " | sed '$ s/$/\n/'
01 02 03 04 05 06 07 08 09 10

 

4、測試4

[root@centos7 test2]# cat a.txt 01
02
03
04
05
06
07
08
09
10 [root@centos7 test2]# awk BEGIN{RS=EOF}'{gsub("\n"," ");print}' a.txt 01 02 03 04 05 06 07 08 09 10

 

5、測試5

[root@centos7 test2]# cat a.txt 01
02
03
04
05
06
07
08
09
10 [root@centos7 test2]# awk '{printf("%s ", $0)}END{printf("\n")}' a.txt 01 02 03 04 05 06 07 08 09 10

 

6、測試6、

[root@centos7 test2]# cat a.txt 01
02
03
04
05
06
07
08
09
10 [root@centos7 test2]# sed ':a;N;s/\n/ /;ta' a.txt 01 02 03 04 05 06 07 08 09 10

 

7、測試7

[root@centos7 test2]# cat a.txt 01
02
03
04
05
06
07
08
09
10 [root@centos7 test2]# awk 'BEGIN{RS="\n"; ORS=" "}{print}END{printf("\n")}' a.txt 01 02 03 04 05 06 07 08 09 10

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM