transpose-file

This commit is contained in:
Gleb Koval 2021-11-08 21:18:55 +00:00
parent 27ba58ad99
commit 1b2f15ec8e
No known key found for this signature in database
GPG Key ID: 7C89CDC822F8392B
2 changed files with 13 additions and 0 deletions

3
transpose-file/file.txt Normal file
View File

@ -0,0 +1,3 @@
name age
alice 21
ryan 30

10
transpose-file/sol.sh Executable file
View File

@ -0,0 +1,10 @@
#!/bin/sh
lines=$(grep -oP '(?<![^ \n])[^ \n]+(?![^ \n])' file.txt)
n=$(sed "1q;d" file.txt | wc -w)
top=$(expr $n - 1)
for i in $(seq 1 $top); do
echo $(echo $lines | tr " " "\n" | awk "NR % $n == $i")
done
echo $(echo $lines | tr " " "\n" | awk "NR % $n == 0")