linux中将矩阵数据转换为一行数据
1、测试数据
root@PC1:/home/test# ls a.txt root@PC1:/home/test# cat a.txt ## 测试数据 i u k f 2 3 8 7 s j 9 4
形式一、按行排列
2、xargs实现
root@PC1:/home/test# ls a.txt root@PC1:/home/test# cat a.txt i u k f 2 3 8 7 s j 9 4 root@PC1:/home/test# cat a.txt | xargs i u k f 2 3 8 7 s j 9 4
3、awk实现
(1)、
root@PC1:/home/test# ls a.txt root@PC1:/home/test# cat a.txt i u k f 2 3 8 7 s j 9 4 root@PC1:/home/test# cat a.txt | awk '{printf("%s ", $0)} END {printf("\n")}' i u k f 2 3 8 7 s j 9 4
(2)、
root@PC1:/home/test# ls a.txt root@PC1:/home/test# cat a.txt i u k f 2 3 8 7 s j 9 4 root@PC1:/home/test# cat a.txt | awk '{ORS = " "} {print $0} END {printf("\n")}' i u k f 2 3 8 7 s j 9 4
(3)、
root@PC1:/home/test# ls a.txt root@PC1:/home/test# cat a.txt i u k f 2 3 8 7 s j 9 4 root@PC1:/home/test# cat a.txt | awk 'BEGIN{RS = EOF} {gsub("\n", " "); print}' i u k f 2 3 8 7 s j 9 4
4、paste实现
root@PC1:/home/test# ls a.txt root@PC1:/home/test# cat a.txt i u k f 2 3 8 7 s j 9 4 root@PC1:/home/test# cat a.txt | paste -s i u k f 2 3 8 7 s j 9 4
5、sed实现
root@PC1:/home/test# ls a.txt root@PC1:/home/test# cat a.txt i u k f 2 3 8 7 s j 9 4 root@PC1:/home/test# cat a.txt | sed ':a; N; s/\n/ /; ta' i u k f 2 3 8 7 s j 9 4
6、tr实现
root@PC1:/home/test# ls a.txt root@PC1:/home/test# cat a.txt i u k f 2 3 8 7 s j 9 4 root@PC1:/home/test# cat a.txt | tr "\n" " " | sed 's/$/\n/' i u k f 2 3 8 7 s j 9 4
R实现:
(1)、
> dir() [1] "a.txt" > dat <- read.table("a.txt", header = F) > dat V1 V2 V3 V4 1 i u k f 2 2 3 8 7 3 s j 9 4 > res <- data.frame("x") > dim(res) [1] 1 1 > for (i in 1:nrow(dat)) { + res <- cbind(res, dat[i,]) + } > res <- res[,-1] > res V1 V2 V3 V4 V1.1 V2.1 V3.1 V4.1 V1.2 V2.2 V3.2 V4.2 1 i u k f 2 3 8 7 s j 9 4
(2)、
> dir() [1] "a.txt" > dat <- read.table("a.txt", header = F) > dat V1 V2 V3 V4 1 i u k f 2 2 3 8 7 3 s j 9 4 > res <- as.matrix(dat)[1:(nrow(dat) * ncol(dat))] > res [1] "i" "2" "s" "u" "3" "j" "k" "8" "9" "f" "7" "4" > res <- t(as.data.frame(res)) > res [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] res "i" "2" "s" "u" "3" "j" "k" "8" "9" "f" "7" "4"
形式二、按列排列
shell实现
root@PC1:/home/test# ls a.txt root@PC1:/home/test# cat a.txt i u k f 2 3 8 7 s j 9 4 root@PC1:/home/test# for i in `seq $(head -n 1 a.txt | awk '{print NF}')`; do cut -d " " -f $i a.txt >> b.txt; done root@PC1:/home/test# cat b.txt i 2 s u 3 j k 8 9 f 7 4 root@PC1:/home/test# cat b.txt | paste -s -d " " i 2 s u 3 j k 8 9 f 7 4
R实现:
> dir() [1] "a.txt" > dat <- read.table("a.txt", header = F) > dat V1 V2 V3 V4 1 i u k f 2 2 3 8 7 3 s j 9 4 > res <- vector() > for (i in 1:ncol(dat)) { + res <- c(res, dat[,i]) + } > res [1] "i" "2" "s" "u" "3" "j" "k" "8" "9" "f" "7" "4" > res <- t(as.data.frame(res)) > res [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] res "i" "2" "s" "u" "3" "j" "k" "8" "9" "f" "7" "4"
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
2021-03-22 c语言中数组中元素的个数