0

I have a text file and I would like to extract the two lines of data following a line containing only "Name" as a value.

Name

032

Express intent to engage in diplomatic cooperation (such as policy

support)

Description

Name

033

Express intent to provide material aid, not specified below

Description

So, the output from above would be a file that has:

032 Express intent to engage in diplomatic cooperation (such as policy support)

033 Express intent to provide material aid, not specified below

7
  • @don_crissti hopefully that does the trick
    – nacc
    Apr 7, 2016 at 19:28
  • Your sample input appears to be double-spaced, is that accurate?
    – Barmar
    Apr 7, 2016 at 19:29
  • Anyway, look at the -A option to grep.
    – Barmar
    Apr 7, 2016 at 19:29
  • 1
    Use the { } code markup for sample input/output, not quotation.
    – Barmar
    Apr 7, 2016 at 19:30
  • @don_crissti I've imagined the scenario thusly and concluded it makes sense to me
    – nacc
    Apr 7, 2016 at 19:31

1 Answer 1

2
awk '/Name/ { readline; num=$0; readline; print num, $0; }' < inputfile

readline reads the next line of the input into $0. So when Name is matched, it reads the next line, puts it into the num variable, then reads the second line, and then prints num and that line together on one line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.