linux中如何将一行数据转换为一列数据
1、测试数据
root@DESKTOP-1N42TVH:/home/test2# ls test.txt root@DESKTOP-1N42TVH:/home/test2# cat test.txt i j k s root@DESKTOP-1N42TVH:/home/test2# sed -n l test.txt i j k s$
2、转换为一列数据
root@DESKTOP-1N42TVH:/home/test2# ls test.txt root@DESKTOP-1N42TVH:/home/test2# cat test.txt i j k s root@DESKTOP-1N42TVH:/home/test2# sed -n l test.txt i j k s$ root@DESKTOP-1N42TVH:/home/test2# sed 's/ /\n/g' test.txt ## sed实现 i j k s root@DESKTOP-1N42TVH:/home/test2# cat test.txt | tr " " "\n" ## tr实现 i j k s root@DESKTOP-1N42TVH:/home/test2# cat test.txt | xargs -n 1 ## xargs实现 i j k s root@DESKTOP-1N42TVH:/home/test2# awk '{gsub(" ", "\n"); print $0}' test.txt ## awk实现 i j k s root@DESKTOP-1N42TVH:/home/test2# awk '{for(i = 1; i <= NF; i++) printf("%s\n", $i)}' test.txt ## awk实现 i j k s
root@DESKTOP-1N42TVH:/home/test2# ls test.txt root@DESKTOP-1N42TVH:/home/test2# cat test.txt i j k s root@DESKTOP-1N42TVH:/home/test2# sed -n l test.txt i j k s$ root@DESKTOP-1N42TVH:/home/test2# awk 'BEGIN{RS = " "}{print $0}' test.txt i j k s root@DESKTOP-1N42TVH:/home/test2# awk 'BEGIN{RS = " "}{print $0}' test.txt | head -n -1 ## awk实现 i j k s root@DESKTOP-1N42TVH:/home/test2# awk 'BEGIN{RS = " "; ORS = "\n"} {print $0}' test.txt i j k s root@DESKTOP-1N42TVH:/home/test2# awk 'BEGIN{RS = " "; ORS = "\n"} {print $0}' test.txt | sed '$d' ## awk实现 i j k s