First, I have what I believe to be a workable solution. However, test cases
are one thing …reality is not always so kind. This is a “Does this look right?”
question …or, better, “Where might this fail? Suggest improvements?” question.
Problem:
Title should not creep past one line.
Test file:
You have a hold available for pickup as of 2012-01-13:
Title: Really Long Test Title Regarding Random Gibberish. Volume 1, A-B, United States
and affiliated territories, United Nations, countries of the world
Author: Barrel Roll Morton
Copy: 3
#end-of-record
You have a hold available for pickup as of 2012-01-13:
Title: Short Catalogue of Random Gibberish. Volume 1, A-B, United States
Author: Skippy Credenza
Copy: 12
#end-of-record
Expected output:
You have a hold available for pickup as of 2012-01-13:
Title: Really Long Test Title Regarding Random Gibberish. Volume 1, A-B, United States
Author: Barrel Roll Morton
Copy: 3
#end-of-record
You have a hold available for pickup as of 2012-01-13:
Title: Short Catalogue of Random Gibberish. Volume 1, A-B, United States
Author: Skippy Credenza
Copy: 12
#end-of-record
My solution:
sed -e '/^Title/{N;/\nAuthor:/!{s/\n.*$//}}' test-file.txt
My logic re: above proposed solution
- Look for regex /^Title/
- Grab next line
- If next line does not match /^Author/
- Then search for regex /\n.*$/
- Replace with nada.
Is there a more bullet proof way to do this?
That looks good, but if you don’t have control on how long that first line of text is, you can further truncate it using something like
(You don’t need the -e, but it doesn’t hurt either).
I use an old-school sed, so I need the
;};}extra-bits.Adjust the number of ‘.’s in the match-pattern for the length of value you want to capture.
Newer sed support curly-braced ranges, something like, cut I don’t have access to confirm.
Edit per @JonathanLeffler ‘s comment below. fixed range notation, change 30,50 to values that work for you.
I hope this helps.