Транспонировать строки в столбцы, сохраняя некоторые строки как ссылки в оболочке - PullRequest
2 голосов
/ 10 июня 2019

У меня есть файл, который мне нужно транспонировать, он выглядит так:

Person ID| commute to work in hours?| Happiness score?| work satisfaction score?
1234| 2 | 8 | 7
5678| 1 | 7 | 6
6789| 0.5 | 9 | 6

Мне нужно повернуть это как показано ниже:

Person ID | Question | Answer
1234 | commute to work in hours? | 2
1234 | Happiness score? | 8
1234 | work satisfaction score? | 7
5678 | commute to work in hours? | 1
5678 | Happiness score? | 7
5678 | work satisfaction score? | 6
6789 | commute to work in hours? | 0.5
6789 | Happiness score? | 9
6789 | work satisfaction score? | 6

Я пытался использовать awk и могу получить:

1234 | 3
1234 | 8
1234 | 7
5678 | 1
5678 | 7
5678 | 6
6789 | 0.5
6789 | 9
6789 | 6

Но я не могу добавить вопрос.

awk '{
   for(i=2;i<=NF;i++) 
       unique[$1]=(unique[$1]FS$i); next } END { 
  for (i in unique) { 
       n=split(unique[i],temp); 
       for(j=1;j<=n;j++) 
           print i,temp[j] } }' file

Ответы [ 3 ]

2 голосов
/ 10 июня 2019

Если у вас есть только три поля, вам не нужно использовать цикл, поэтому это должно работать.

awk -F\| 'NR==1 {a=$2;b=$3;c=$4;print $1," Question | Answer";next} {print $1,a,$2"\n"$1,b,$3"\n"$1,c,$4}' OFS=" |" file
Person ID | Question | Answer
1234 | commute to work in hours? | 2
1234 | Happiness score? | 8
1234 | work satisfaction score? | 7
5678 | commute to work in hours? | 1
5678 | Happiness score? | 7
5678 | work satisfaction score? | 6
6789 | commute to work in hours? | 0.5
6789 | Happiness score? | 9
6789 | work satisfaction score? | 6
1 голос
/ 10 июня 2019
$ cat tst.awk
BEGIN { FS=" *[|] *"; OFS=" | " }

NR==1 {
    split($0,qs)
    print $1, "Question", "Answer"
    next
}

{
    for (i=2; i<=NF; i++) {
        print $1, qs[i], $i
    }
}

$ awk -f tst.awk file
Person ID | Question | Answer
1234 | commute to work in hours? | 2
1234 | Happiness score? | 8
1234 | work satisfaction score? | 7
5678 | commute to work in hours? | 1
5678 | Happiness score? | 7
5678 | work satisfaction score? | 6
6789 | commute to work in hours? | 0.5
6789 | Happiness score? | 9
6789 | work satisfaction score? | 6
1 голос
/ 10 июня 2019

Ввод:

$ cat file_transpose 
Person ID| commute to work in hours?| Happiness score?| work satisfaction score?
1234| 2 | 8 | 7
5678| 1 | 7 | 6
6789| 0.5 | 9 | 6

Выход:

$ awk 'BEGIN{FS="|";OFS=" |"}NR==1{for(i=2;i<=NF;i++){buff[i]=$i};print "Person ID | Question | Answer";next}{for(i=2;i<=NF;i++){print $1,buff[i],$i}}' file_transpose 
Person ID | Question | Answer
1234 | commute to work in hours? | 2 
1234 | Happiness score? | 8 
1234 | work satisfaction score? | 7
5678 | commute to work in hours? | 1 
5678 | Happiness score? | 7 
5678 | work satisfaction score? | 6
6789 | commute to work in hours? | 0.5 
6789 | Happiness score? | 9 
6789 | work satisfaction score? | 6

Пояснение:

#field separator and output field separator
BEGIN {
       FS = "|"
       #you might want to remove the space
       OFS = " |"
}

# Rule(s)
# On the first line, save all the questions in buff, print the header, jump to next line
NR == 1 { 
       for (i = 2; i <= NF; i++) {
            buff[i] = $i
       }
       print "Person ID | Question | Answer"
       next
 }
 #for the rest of the file, print the first field, each question and associated answer
 {
       for (i = 2; i <= NF; i++) {
           print $1, buff[i], $i
       }
 }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...