Reputation: 394
I would like to reformat the content of the text file using Scala such as for the given sample file:
"good service"
Tom Martin (USA) 17th October 2015
4
Hi my name is
Tom.
I love boardgames.
Aircraft TXT-102
"not bad"
M Muller (Canada) 22nd September 2015
6
Hi
I
like
boardgames.
Aircraft TXT-101
Type Of Customer Couple Leisure
Cabin Flown FirstClass
Route IND to CHI
Date Flown September 2015
Seat Comfort 12345
Cabin Staff Service 12345
.
.
Gets reformated to this:
"good service"
Tom Martin (USA) 17th October 2015
4
Hi my name is Tom. I love boardgames.
Aircraft TXT-102
"not bad"
M Muller (Canada) 22nd September 2015
6
Hi I like boardgames.
Aircraft TXT-101
Type Of Customer Couple Leisure
Cabin Flown FirstClass
Route IND to CHI
Date Flown September 2015
Seat Comfort 12345
Cabin Staff Service 12345
.
.
I have identified the pattern of my file, which is: This multi-line string comes between a digit and word separated by tabs.
For example, first block's multi line content comes between 4 and Aircraft TXT-102
. Second block's multi line content comes between 6 and Aircraft TXT-101
Also, blocks are delimited by two new lines.
I know of Pattern matching using regex can help but I do not know how to handle this on file.
Upvotes: 0
Views: 15
Reputation: 40894
What I'd do, in pseudocode:
while more lines available {
lines_so_far = read input until a number is seen
output(lines_so_far)
lines_to_join = read input until "Aircraft" is seen
output(joined lines_to_join)
}
A regexp for a line consisting of only a number is ^\d+$
; for a line starting with "Airline", ^Airline .*
. The convenience method to look at is takeWhile
.
Upvotes: 1