Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »

Review Suggested Edit

You can't approve or reject suggested edits because you haven't yet earned the Edit Posts ability.

Approved.
This suggested edit was approved and applied to the post 12 months ago by Canina‭.

12 / 255
How to extract string from file, run filter, and replace in file with new value?
  • **TASK**
  • I am coding up ebooks to a specific standard, and have a script that converts a string into the correct titlecase for this publisher. When working with some public domain source files, one often gets this for a chapter title string:
  • &emsp;&emsp;&emsp;`<p>HERE IS MY TITLE</p>`
  • Using VSCodium (FOSS VS Code alternative), I can open each file, select the string between the `p` tags, then run the `titlecase` script with a hotkey that I've assigned it to. I end up with
  • &emsp;&emsp;&emsp;`<p>Here Is My Title</p>`
  • (VSCodium's native titlecase filter isn't up to this job.) I save the file, and go on to the next one.
  • If you only have a few of these to do, that's fine. But sometimes there can be dozens, and it gets very tedious.
  • **QUESTION**
  • Is there a way that I can script this? I have scratched my head over both `awk` and `sed`, thinking that these are my prime options. But (as a rank amateur) I cannot work out how to:
  • - iterate through all `chapter-*.xhtml` files in a directory,
  • - extract my string (ALWAYS line 12 in the file, only string on line, between `<p>...</p>` tags),
  • - **run my "external" `titlecase` filter on that string**,
  • - replace the new string for the original one in the source file,
  • - for all those files. :)
  • (The step in bold is the one that is my biggest stumbling block.)
  • I would be grateful for any help with this! If I've omitted any relevant information, please say and I'll remedy that a.s.a.p.
  • David / Fife, UK
  • **TASK**
  • I am coding up ebooks to a specific standard, and have a script that converts a string into the correct titlecase for this publisher. When working with some public domain source files, one often gets this for a chapter title string:
  • &emsp;&emsp;&emsp;`<p>HERE IS MY TITLE</p>`
  • Using VSCodium (FOSS VS Code alternative), I can open each file, select the string between the `p` tags, then run the `titlecase` script with a hotkey that I've assigned it to. I end up with
  • &emsp;&emsp;&emsp;`<p>Here Is My Title</p>`
  • (VSCodium's native titlecase filter isn't up to this job.) I save the file, and go on to the next one.
  • If you only have a few of these to do, that's fine. But sometimes there can be dozens, and it gets very tedious.
  • **QUESTION**
  • Is there a way that I can script this? I have scratched my head over both `awk` and `sed`, thinking that these are my prime options. But (as a rank amateur) I cannot work out how to:
  • - iterate through all `chapter-*.xhtml` files in a directory,
  • - extract my string (ALWAYS line 12 in the file, only string on line, between `<p>...</p>` tags),
  • - **run my "external" `titlecase` filter on that string**,
  • - replace the new string for the original one in the source file,
  • - for all those files. :)
  • (The step in bold is the one that is my biggest stumbling block.)