awk variable not printed correctly concatenated (second string overwritting the first)

时间:2019-04-17 02:35:42

标签: bash awk

I always start writing an awk script thinking it's simple, but find myself baffled by the strange result.. This time again..

I have a file list.txt with lines below (reduced for test. The line following 'Preview Abstract' is the abstract, and the following 'View details' is the paper title.)

Google Scholar
Copy BibTex
Preview Abstract
This is the first paper's abstract.
View details
From Language to Goals: Inverse Reinforcement Learning for Vision-Based Instruction Following
Justin Fu, Anoop Korattikara, Sergey Levine, Sergio Guadarrama  International Conference on Learning Representations (ICLR) (2019)

Google Scholar
Copy BibTex
Preview Abstract
This is the second paper's abstract.
View details
A Study on Overfitting in Deep Reinforcement Learning
Chiyuan Zhang, Oriol Vinyals, Remi Munos, Samy Bengio  arXiv (2018)

Google Scholar
Copy BibTex
Preview Abstract
This is the third papers's abstract.
View details
Ask the Right Questions: Active Question Reformulation with Reinforcement Learning
Christian Buck, Jannis Bulian, Massimiliano Ciaramita, Wojciech Pawe©© Gajewski, Andrea Gesmundo, Neil Houlsby, Wei Wang  Sixth International Conference on Learning Representations (2018)

and I want to get a file like this (in "title ## abstract" format).

From Language to Goals: Inverse Reinforcement Learning for Vision-Based Instruction Following ## This is the first paper's abstract.
A Study on Overfitting in Deep Reinforcement Learning ## This is the second paper's abstract.
Ask the Right Questions: Active Question Reformulation with Reinforcement Learning ## This is the third papers's abstract.

So I wrote this awk script (paper.awk). (uses keyword to keep title and abstrct lines and print them in the wanted format after getting the paper title)

print_res == 1 {print title " ## " abs; print_res = 0}
next_abs == 1 {abs = $0; next_abs = 0}
next_title == 1 {title = $0; next_title = 0; print_res = 1}
/Preview Abstract/{next_abs = 1}
/View details/{next_title = 1}

When I run awk -f paper.awk list.txt, I get

 ## This is the first paper's abstract.cement Learning for Vision-Based Instruction Following
 ## This is the second paper's abstract.ment Learning
 ## This is the third papers's abstract. Reformulation with Reinforcement Learning

Somehow the paper titles are overwritten with the ' ## abstract" string. What's wrong?

0 个答案:

没有答案