admin管理员组文章数量:1122832
have a repository containing the README.md
, and a folder called sd/
. In this folder are a bunch of files; I'd like to extract a list of those whose names follow the pattern sd_YYY-MM-DD.md
, and update the readme with this list of files in reverse chronological order (newest first), so that the readme looks like:
[the usual content of the readme]
# Current files
[21/11/2024](sd/sd_2024-11-21.md)
etc.
I've managed to work out that my regex should be /sd_\d{4}(-\d{2}){2}\.md/gm
, and I think the way to use this is in the command
find sd/ -regex "/sd_\d{4}(-\d{2}){2}\.md/gm"
but I can't work out how to extract the date from this, much less how to update a list in the readme (rather than appending every time).
My best attempt at setting up the workflow is as follows:
name: update-sd-list
on: [push]
jobs:
get-file-list-and-write-to-readme:
runs-on: ubuntu-latest
steps:
- run: [?????????]
Maybe I also need to commit the change?
Stack Overflow suggests README has failed to update using Github Action which is close to what I want but I don't understand it well enough to modify it to my needs (and it doesn't include the getting-the-date part).
have a repository containing the README.md
, and a folder called sd/
. In this folder are a bunch of files; I'd like to extract a list of those whose names follow the pattern sd_YYY-MM-DD.md
, and update the readme with this list of files in reverse chronological order (newest first), so that the readme looks like:
[the usual content of the readme]
# Current files
[21/11/2024](sd/sd_2024-11-21.md)
etc.
I've managed to work out that my regex should be /sd_\d{4}(-\d{2}){2}\.md/gm
, and I think the way to use this is in the command
find sd/ -regex "/sd_\d{4}(-\d{2}){2}\.md/gm"
but I can't work out how to extract the date from this, much less how to update a list in the readme (rather than appending every time).
My best attempt at setting up the workflow is as follows:
name: update-sd-list
on: [push]
jobs:
get-file-list-and-write-to-readme:
runs-on: ubuntu-latest
steps:
- run: [?????????]
Maybe I also need to commit the change?
Stack Overflow suggests README has failed to update using Github Action which is close to what I want but I don't understand it well enough to modify it to my needs (and it doesn't include the getting-the-date part).
Share Improve this question asked Nov 21, 2024 at 10:11 confusedandbemusedconfusedandbemused 231 silver badge2 bronze badges1 Answer
Reset to default 1Given the date format in the filenames, a glob will expand them in ascending order, so you could build the list of new links with something like this:
printf '%s\n' sd/sd_*-*-*.md |
tac |
awk -F '[_.-]' '{printf("[%s/%s/%s](%s)\n", $4,$3,$2,$0)}'
Now, updating README.md
depends on the exact structure of the file, but skipping the old links and adding the new ones after the # Current files
line should be enough here:
# previous_commands |
awk '
NR == 1 {
fileLinks = $0;
next;
}
$0 == "# Current files" {
printf("%s\n\n%s", $0, fileLinks);
next;
}
/\(sd\/sd_.*\.md\)/ { next } # skip old links
{ print }
' RS= - RS='\n' README.md > README.md.new # && mv README.md.new README.md
Then, how you'll add the above code in your workflow is up to you.
ASIDE
Here's an other solution, POSIX compliant, with a single call to awk
:
printf '%s\n' sd/sd_*-*-*.md |
sort -r |
awk -F '[_.-]' '
# process STDIN stream
FNR == NR {
links[++nbLinks] = sprintf("[%s/%s/%s](%s)", $4,$3,$2,$0);
next;
}
# process README.md file
$0 == "# Current files" {
print;
for (i = 1; i <= nbLinks; i++)
printf("\n%s", links[i]);
next;
}
/\(sd\/sd_.*\.md\)/ { next }
{ print }
' - README.md > README.md.tmp &&
mv README.md.tmp README.md
本文标签: bashHow to update list of files in READMEStack Overflow
版权声明:本文标题:bash - How to update list of files in README - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736311922a1934913.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论