首页 > 系统相关 >linux中利用awk命令将一列数据转换为一行数据

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

时间:2022-12-08 12:34:16浏览次数:37  
标签:root PC1 cat awk linux test txt 数据

 

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$

 

标签:root,PC1,cat,awk,linux,test,txt,数据
From: https://www.cnblogs.com/liujiaxin2018/p/16965759.html

相关文章