sed - Adding a text from one file when finding a match on another file -


i search through many answers , not find on it.

i have text file (from exam) , answers in file. managed add text "answer: " @ end of each question, cannot find way pick answer file2 file1.

file1:

question: 179 – alterando-se o ângulo de ataque de 0º para 6º, resistência parasita:

a aumenta  b) não se altera  c) diminui  d) impossível de se determinar  answer: 

file2:

177)c 178)a 179)b 180)b 

i tried use sed far no success, suggestion appreciated.

the file1 structure repeats every question, questions can have more 1 line.

the desired output should be:

question: 179 – alterando-se o ângulo de ataque de 0º para 6º, resistência parasita:

a aumenta  b) não se altera  c) diminui  d) impossível de se determinar  answer: b 

sed hardly tool of choice this, whereas reasonably easy in awk.

awk 'nr==fnr {         # nr equal fnr when reading first input file         # store right answer each question in array         split($1, b, /\)/)         # if input 123)a, array b contains "123" , "a"         a[b[1]] = b[2]         # done; skip next line         next     }     # if here, in second file.  find question delimiter     /question: [0-9]+/ {         # if have previous question in memory, print answer first         if (q>0) { print "\nanswer: " a[q] "\n\n" }         # remember index question         q=$2     }     # if far, perform default action, is, print line     # "1" shorthand "print current line"     1     # @ end of file, print last remaining answer     end { print "\nanswer: " a[q] "\n\n" }' file2 file1 

this isn't entirely robust if format of question header or data in answers file isn't regular, though.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -