linux中利用awk命令将一列数据转换为一行数据

 

001、 

[root@PC1 test]# ls
a.txt
[root@PC1 test]# cat a.txt            ## 测试数据
1
2
3
4
5
6
[root@PC1 test]# awk '{printf("%s ", $0)} END {printf"\n"}' a.txt
1 2 3 4 5 6
[root@PC1 test]# awk '{printf("%s ", $0)} END {printf"\n"}' a.txt | cat -A
1 2 3 4 5 6 $
[root@PC1 test]# awk '{printf("%s ", $0)} END {printf"\n"}' a.txt | sed 's/.$//' | cat -A
1 2 3 4 5 6$

 

 

002、

[root@PC1 test]# ls
a.txt
[root@PC1 test]# cat a.txt
1
2
3
4
5
6
[root@PC1 test]# awk 'BEGIN{RS = "$@#"}{gsub("\n", " "); print $0}' a.txt
1 2 3 4 5 6
[root@PC1 test]# awk 'BEGIN{RS = "$@#"}{gsub("\n", " "); print $0}' a.txt | sed -n l
1 2 3 4 5 6 $
[root@PC1 test]# awk 'BEGIN{RS = "$@#"}{gsub("\n", " "); print $0}' a.txt | sed 's/.$//' | sed -n l
1 2 3 4 5 6$

 

 

 003、

[root@PC1 test]# ls
a.txt
[root@PC1 test]# cat a.txt
1
2
3
4
5
6
[root@PC1 test]# awk 'BEGIN{RS = EOF} {gsub("\n", " "); print $0}' a.txt
1 2 3 4 5 6
[root@PC1 test]# awk 'BEGIN{RS = EOF} {gsub("\n", " "); print $0}' a.txt | cat -A
1 2 3 4 5 6$

 

 

004、

[root@PC1 test]# ls
a.txt
[root@PC1 test]# cat a.txt
1
2
3
4
5
6
[root@PC1 test]# awk '{ORS = " "; print $0}' a.txt | sed 's/.$/\n/'
1 2 3 4 5 6
[root@PC1 test]# awk '{ORS = " "; print $0}' a.txt | sed 's/.$/\n/' | cat -A
1 2 3 4 5 6$

 

posted @ 2022-12-08 12:21  小鲨鱼2018  阅读(589)  评论(0编辑  收藏  举报