2

I have a text file file1.txt on Unix. I'd like to produce another file file2.txt, in which I change all groups of lines that has this format (taken from a multiple-choice exam)

a. [first choice]
b. [second choice]
c. [third choice]

to

[first choice] [second choice] [third choice]

How could I do that?

EDIT: An example is

What is the value of three plus five?
a. six
b. seven
c. eight

This line is not so relevant.
blah blah

What is the capital of England?
a. London
b. Birmingham
c. New York

It should be converted to

What is the value of three plus five?
six seven eight

This line is not so relevant.
blah blah

What is the capital of England?
London Birmingham New York    
2
  • is your input complete? so the file2 will contain only one line? just join all [...] together? How does the "group" in file1 look like? Commented Dec 1, 2012 at 20:20
  • @Kent Sorry, seems like my description wasn't clear. Please see my example. Commented Dec 1, 2012 at 20:23

1 Answer 1

3

this one-liner should work for you:

 awk '{if($0~/^[a-z]\. /){gsub(/^[a-z]\. /,"");printf "%s ",$0;s=1;next;}else{if(s)print "";print $0;s=0}}' file1

test

kent$  cat exam
What is the value of three plus five?
a. six
b. seven
c. eight

This line is not so relevant.
blah blah

What is the capital of England?
a. London
b. Birmingham
c. New York

kent$  awk '{if($0~/^[a-z]\. /){gsub(/^[a-z]\. /,"");printf "%s ",$0;s=1;next;}else{if(s)print "";print $0;s=0}}' exam
What is the value of three plus five?
six seven eight 

This line is not so relevant.
blah blah

What is the capital of England?
London Birmingham New York  
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.