Wednesday 1 January 2020

unix - Converting strings in input file


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

Answer



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.


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...