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"  

 

posted @ 2022-03-22 23:36  小鲨鱼2018  阅读(130)  评论(0编辑  收藏  举报