LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-21-2022, 07:49 AM   #1
ychaouche
Member
 
Registered: Mar 2017
Distribution: Mint, Debian, Q4OS, Mageia, KDE Neon
Posts: 246
Blog Entries: 1

Rep: Reputation: 19
Cool awk/sed/bash script to display last changelog entry


So I have a file that resembles a changelog in the sense that it contains dated entries.
Each entry starts with a "*", then with a free format date, then a newline and a set of
changes. A sample :

Code:
* Dimanche 18 Septembre 2022
  - fix package.files.list
  - split brain solution	
  - redouane : envoi vers dste
  - réfléchir à une solution
  - proposition : enlever du carnet d'addresse gloable + personnel + automatique de la secrétaire, ajouter au carnet d'addresse personnel de chaque boite des brigadiers de la DSTE.
  - notes.log.search
  - notes.log.search.recent

* Lundi 19 Septembre 2022
  - DNS override
  - ...
  - ...
There's a guarentee that each line starting with "*" will be a new entry.

I'd like to write a script that would display the last entry in the file. If I take last
sample as example, it would display this :

Code:
* Lundi 19 Septembre 2022
  - DNS override
  - ...
  - ...
Any ideas? bonus points if you can do it in sed using its hold space.
 
Old 09-21-2022, 07:51 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 6,417
Blog Entries: 3

Rep: Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359Reputation: 3359
You could do it with AWK or Perl with the pattern ^\* as the record separator. Then process the file without printing but have the END { } block print the last record read.
 
1 members found this post helpful.
Old 09-21-2022, 07:53 AM   #3
ychaouche
Member
 
Registered: Mar 2017
Distribution: Mint, Debian, Q4OS, Mageia, KDE Neon
Posts: 246

Original Poster
Blog Entries: 1

Rep: Reputation: 19
Something like

Quote:
awk --<record-separator-option>='*' "END {print}"
?
 
Old 09-21-2022, 07:55 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 20,534

Rep: Reputation: 3889Reputation: 3889Reputation: 3889Reputation: 3889Reputation: 3889Reputation: 3889Reputation: 3889Reputation: 3889Reputation: 3889Reputation: 3889Reputation: 3889
Quote:
Originally Posted by ychaouche View Post
Any ideas? bonus points if you can do it in sed using its hold space.
Sure - use awk. It has a simple means of treating everything between blank (null) lines as a single record - read the doco. No bonus points for needless contortions.
 
1 members found this post helpful.
Old 09-21-2022, 07:55 AM   #5
ychaouche
Member
 
Registered: Mar 2017
Distribution: Mint, Debian, Q4OS, Mageia, KDE Neon
Posts: 246

Original Poster
Blog Entries: 1

Rep: Reputation: 19
Perfect!

Quote:
$ awk -v RS="*" "END {print}" < ~/NOTES/LOG/worklog.summary
Lundi 19 Septembre 2022
- DNS override

$
 
Old 09-21-2022, 07:57 AM   #6
ychaouche
Member
 
Registered: Mar 2017
Distribution: Mint, Debian, Q4OS, Mageia, KDE Neon
Posts: 246

Original Poster
Blog Entries: 1

Rep: Reputation: 19
Fastest thread closing I've head in my life.
 
Old 09-21-2022, 08:22 AM   #7
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 2,795

Rep: Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049
Quote:
Originally Posted by ychaouche View Post
There's a guarentee that each line starting with "*" will be a new entry.

I'd like to write a script that would display the last entry in the file.
This says that records are separated by "\n*" not by just "*" - if the lines can have asterisks inline it will fail.

You can solve that issue with a regex in the RS variable:
Code:
awk -vRS='\n\\*' 'END{print}' ~/NOTES/LOG/worklog.summary
(This is supported in gawk and mawk, plus BWK awk since 2019; other awk implementations may or not have it.)


Also, I'm not sure whether any versions of Awk will detect you only want the last record and optimize or whether it will still parse the whole file - if the latter and these are long files, you may want to use a method that specifically walks backwards from the end instead.


Last edited by boughtonp; 09-21-2022 at 08:24 AM.
 
1 members found this post helpful.
Old 09-21-2022, 08:29 AM   #8
ychaouche
Member
 
Registered: Mar 2017
Distribution: Mint, Debian, Q4OS, Mageia, KDE Neon
Posts: 246

Original Poster
Blog Entries: 1

Rep: Reputation: 19
Good catch!
Quote:
$ awk -v RS='\n\\*' "END {print}" < ~/NOTES/LOG/worklog.summary
Lundi 19 Septembre 2022
- DNS override
* this is not true !

is it?

$ awk -v RS='\\*' "END {print}" < ~/NOTES/LOG/worklog.summary
this is not true !

is it?
$
 
Old 09-21-2022, 08:32 AM   #9
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 2,795

Rep: Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049Reputation: 2049

(Also note that Awk accepts a filename, so the stdin redirector isn't needed here.)

 
Old 09-21-2022, 08:33 AM   #10
ychaouche
Member
 
Registered: Mar 2017
Distribution: Mint, Debian, Q4OS, Mageia, KDE Neon
Posts: 246

Original Poster
Blog Entries: 1

Rep: Reputation: 19
thanks! learning so much today. XP++
 
Old 09-21-2022, 05:08 PM   #11
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,094

Rep: Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939Reputation: 939
Code:
sed -n 'H;/^[*]/h;$x;$p'
No default print.
Append input space to hold space
* at the beginning: copy input space to hold space
Last line: move hold space to input space
Last line: print input space
 
2 members found this post helpful.
Old 09-22-2022, 04:13 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,944

Rep: Reputation: 3168Reputation: 3168Reputation: 3168Reputation: 3168Reputation: 3168Reputation: 3168Reputation: 3168Reputation: 3168Reputation: 3168Reputation: 3168Reputation: 3168
I believe syg00 was referring to something like:
Code:
awk 'END{print}' RS='\n\n' file
 
1 members found this post helpful.
Old 09-22-2022, 05:03 AM   #13
ychaouche
Member
 
Registered: Mar 2017
Distribution: Mint, Debian, Q4OS, Mageia, KDE Neon
Posts: 246

Original Poster
Blog Entries: 1

Rep: Reputation: 19
Thanks. There's no guarantee that records are separated by blank lines though.
 
Old 09-22-2022, 06:00 AM   #14
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 20,534

Rep: Reputation: 3889Reputation: 3889Reputation: 3889Reputation: 3889Reputation: 3889Reputation: 3889Reputation: 3889Reputation: 3889Reputation: 3889Reputation: 3889Reputation: 3889
The data you presented indicated it was. Easy enough to rectify - I've been known to use sed then pipe it to awk similar to grail's. More quickly knocked up - and perhaps more readable - than MadeInGermany's exemplary offering.
 
Old 09-22-2022, 08:26 AM   #15
ychaouche
Member
 
Registered: Mar 2017
Distribution: Mint, Debian, Q4OS, Mageia, KDE Neon
Posts: 246

Original Poster
Blog Entries: 1

Rep: Reputation: 19
But adding a subprocess to the pipeline. I often prefere to reduce the number of pipes, use sed
when possible, and use awk when sed is just too complicated.
 
  


Reply

Tags
awk, bash, changelog, sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Get first day of last month and last day of last month in bash xowl Linux - Software 18 02-09-2017 09:49 AM
[SOLVED] sed inside awk or awk inside awk maddyfreaks Linux - Newbie 4 06-29-2016 01:10 PM
[SOLVED] Grabbing the last field of a delimited entry without using "awk" or "sed" drandre Linux - Newbie 17 04-18-2012 10:54 PM
Mirror script missed last 64-current changelog update Biggen Slackware 4 07-20-2009 06:16 PM
From today's changelog entry mscole Slackware 10 03-21-2008 10:55 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration