Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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    
share|improve this question
    
easiest is to use awk or perl to do that –  Tomas Dec 1 '12 at 20:28

1 Answer 1

up vote 2 down vote accepted

Assuming it's always 3 choices, a., b. and c., try this:

sed '/^[a-c]\. /{N;N;s/[a-c]\. / /g;s/[\r\n]//g;s/^ //}' file1.txt > file2.txt

This works by grabbing up three lines at a time using the N command, substituting a space for all the a., b. and c. occurrences, deleting all the line ends and, finally, deleting the last remaining extra space at the beginning of the line.

share|improve this answer
    
Nicole, thanks for your answer! Could you please provide a version for 5 choices too (a., b., c., d., and e.)? Right now I don't understand your command, so I can't modify it myself, but I promise I'll study it soon. –  Mika H. Dec 1 '12 at 20:34
    
If it's only just always 5 choices, not 3, then just add another two N; commands into it. If it's a variable number of choices from one question to the next, it can be done by iterating (looping) and using the hold buffer, but that's a lot of work. –  Nicole Hamilton Dec 1 '12 at 21:00
1  
And [a-c] to [a-e] ? (Also trying to solve this as a way to learn) –  Hennes Dec 1 '12 at 21:01
    
LOL. Yes. You got me. :) –  Nicole Hamilton Dec 1 '12 at 21:07

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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