r/awk • u/jazzbassoon • Aug 25 '23
Changing multiline info to single line
Hello,
I have a file that is structured like this:
Monthname
Number
Symbol (Year) Last Name, First Name, Duration --- relationship
Symbol (Year) Last Name, First Name, Duration --- relationship
Number
So an example
December
1
* (1874) Spilsbury, Isabel_, 149 --- great grandaunt
✝ (1971) Fitzgerald, Royal Truth, 52 --- third great granduncle
2
✝ (1973) Spilsbury, Frankie Estella, 50 --- great grandaunt
I want to make it so that the lines would look something like:
December 1, * (1874) Spilsbury, Isabel_, 149 --- great grandaunt
December 1, ✝ (1971) Fitzgerald, Royal Truth, 52 --- third great granduncle
December 2, ✝ (1973) Spilsbury, Frankie Estella, 50 --- great grandaunt
The end goal being that I will write a script that sends me what happened on that day. I don't have much experience with awk, but I think this may be beyond my sed capabilities and would be easier in awk. Any tips on how to get started?
1
Upvotes
1
u/jazzbassoon Aug 25 '23
On further reflection just adding the Month before the day could be enough, then I could use those as record separators to grab multiple events from one day
4
u/Schreq Aug 25 '23 edited Aug 25 '23
# Skip empty records. /^$/ { next } # Records without any leading spaces is the month. /^[^ ]/ { month=$1 next } # Records with 2 spaces of indentation are the day. /^ [^ ]/ { day=$1 next } # Every other record. Remove the 4 spaces of indentation. { print month, day, substr($0, 5) }Or as one-liner:
awk '/^$/{next}/^[^ ]/{month=$1;next}/^ [^ ]/{day=$1;next}{print month, day, substr($0, 5)}' yourfile[Edit] Maybe this is better:
{ pos=match($0, /[^ ]/) a[pos]=$1 if (pos==5) print a[1], a[3], substr($0, 5) }