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 »
Q&A

Post History

60%
+1 −0
Q&A How to extract string from file, run filter, and replace in file with new value?

Both replies, at different points, provided the basis for this working script. Assuming that the 12th line of file has something like:     <p>HERE IS MY TITLE</p> where HERE... begi...

posted 12mo ago by David‭  ·  edited 12mo ago by David‭

Answer
#2: Post edited by user avatar David‭ · 2023-11-30T10:14:41Z (12 months ago)
clarify one point
  • Both replies, at different points, provided the basis for this working script. Assuming that the 12th line of file has something like:
  • ```
  •     <p>HERE IS MY TITLE</p>
  • ```
  • where `HERE...` begins at column 8 (I need to omit the opening `<p>` tag, as noted in the original post), then:
  • ```
  • for filename in chapter-*.xhtml; do
  • new12=$(sed -n '12p' "$filename" | cut -b 8- | se titlecase -n)
  • sed -i -e '12s#^\(.*<p>\).*#'"\1$new12"'#g' "$filename"
  • done
  • ```
  • The two middle lines work this way:
  • **Line 2**:
  • - `sed -n '12p' "$filename"` = print the 12th line of the file
  • - `cut -b 8-` = "[cut](https://www.man7.org/linux/man-pages/man1/cut.1.html)" from the 8th column
  • - `se titlecase -n` = run the `titlecase` script (`-n` prevents it from generating a "newline")
  • - all that assigned to `$new12`.
  • **Line 3**
  • - `sed -i -e '12s#^\(.*<p>\).*#'"\1$new12"'#g'` = replace original line 12, capturing the first part of the line, up to the opening `<p>` in a backreference group, so `\1` in the "replace", combined with the `$new12` value.
  • Produces:
  • ```
  •     <p>Here Is My Title</p>
  • ```
  • in `"$filename"`. Done. :)
  • Both replies, at different points, provided the basis for this working script. Assuming that the 12th line of file has something like:
  • ```
  •     <p>HERE IS MY TITLE</p>
  • ```
  • where `HERE...` begins at column 8 (I need to omit the opening `<p>` tag, as noted in the original post), then:
  • ```
  • for filename in chapter-*.xhtml; do
  • new12=$(sed -n '12p' "$filename" | cut -b 8- | se titlecase -n)
  • sed -i -e '12s#^\(.*<p>\).*#'"\1$new12"'#g' "$filename"
  • done
  • ```
  • The two middle lines work this way:
  • **Line 2**:
  • - `sed -n '12p' "$filename"` = print the 12th line of the file
  • - `cut -b 8-` = "[cut](https://www.man7.org/linux/man-pages/man1/cut.1.html)" from the 8th column, so in this example, passing the string `HERE IS MY TITLE</p>` to the pipe
  • - `se titlecase -n` = run the `titlecase` script (`-n` prevents it from generating a "newline")
  • - all that assigned to `$new12`.
  • **Line 3**
  • - `sed -i -e '12s#^\(.*<p>\).*#'"\1$new12"'#g'` = replace original line 12, capturing the first part of the line, up to the opening `<p>` in a backreference group, so `\1` in the "replace", combined with the `$new12` value.
  • Produces:
  • ```
  •     <p>Here Is My Title</p>
  • ```
  • in `"$filename"`. Done. :)
#1: Initial revision by user avatar David‭ · 2023-11-30T10:13:05Z (12 months ago)
Both replies, at different points, provided the basis for this working script. Assuming that the 12th line of file has something like:

```
    <p>HERE IS MY TITLE</p>
```

where `HERE...` begins at column 8 (I need to omit the opening `<p>` tag, as noted in the original post), then:    

```
for filename in chapter-*.xhtml; do
    new12=$(sed -n '12p' "$filename" | cut -b 8- | se titlecase -n)
    sed -i -e '12s#^\(.*<p>\).*#'"\1$new12"'#g' "$filename"
  done
```

The two middle lines work this way:

**Line 2**:
- `sed -n '12p' "$filename"` = print the 12th line of the file
- `cut -b 8-` = "[cut](https://www.man7.org/linux/man-pages/man1/cut.1.html)" from the 8th column
- `se titlecase -n` = run the `titlecase` script (`-n` prevents it from generating a "newline")
- all that assigned to `$new12`.

**Line 3**  
- `sed -i -e '12s#^\(.*<p>\).*#'"\1$new12"'#g'` = replace original line 12, capturing the first part of the line, up to the opening `<p>` in a backreference group, so `\1` in the "replace", combined with the `$new12` value.

Produces: 

```
    <p>Here Is My Title</p>
```

in `"$filename"`. Done. :)