Skip to content

Instantly share code, notes, and snippets.

@JV-conseil
Last active June 8, 2024 13:27
Show Gist options
  • Save JV-conseil/681444561e52b4d103e1c3bd8638f34d to your computer and use it in GitHub Desktop.
Save JV-conseil/681444561e52b4d103e1c3bd8638f34d to your computer and use it in GitHub Desktop.
#stackoverflow #superuser #stackexchange

Replace multiline text with Bash

Become a sponsor to JV-conseil Follow JV conseil on StackOverflow Follow JVconseil on Twitter Follow JVconseil on Mastodon Follow JV conseil on GitHub

Here is a Bash script using ed line editor to replace / update a part of multiline text in a text file with automatically generated content (eg: Table of contents) 👇

replace.sh

#!/usr/bin/env bash
# -*- coding: UTF-8 -*-
#
# github   : https://github.com/JV-conseil
# www      : https://www.jv-conseil.net
# author   : JV-conseil
#===============================================
set -Eeou pipefail
shopt -s failglob

_lines=""

for i in {1..10}; do
  _lines+=$'\n'"${i}. ""$(openssl rand -hex 12)"
done

ed -s "./sample.md" <<EOF
/## BEGIN GENERATED/+,/## END GENERATED/-d
/## BEGIN GENERATED/a
${_lines}

.
wq
EOF

sample.md

# How to replace part of a text file between markers with another text file?

Here is a Bash script using [ed line editor](https://www.gnu.org/software/ed/manual/ed_manual.html "GNU manual for ed line editor") to replace / update a part of multiline text in a text file with
automatically generated content (eg: Table of contents) 👇

## BEGIN GENERATED

1. d5c10e45943b196b005771f1
2. bc56de530fbf5342d55203b4
3. 9608f31a09c2ece9c9cee4dd
4. 54e361257aecce0a937211c0
5. c4b2edf88293df0747694c37
6. a80275accc9b2bc72d55efcd
7. 3a2e2fec5da23b415569de00
8. 524655319a05e7dd1f48e6a5
9. 879d17e9e611d163e5a85b02
10. 80d311a786efcd9d01da86f6

## END GENERATED

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris et libero eu nunc
dapibus volutpat. Proin eu neque eu orci volutpat bibendum...

Read the answers on superuser.comstackoverflow.com

#!/usr/bin/env bash
# -*- coding: UTF-8 -*-
#
# github : https://github.com/JV-conseil
# www : https://www.jv-conseil.net
# author : JV-conseil
#===============================================
set -Eeou pipefail
shopt -s failglob
_lines=""
for i in {1..10}; do
_lines+=$'\n'"${i}. ""$(openssl rand -hex 12)"
done
ed -s "./sample.md" <<EOF
/## BEGIN GENERATED/+,/## END GENERATED/-d
/## BEGIN GENERATED/a
${_lines}
.
wq
EOF

How to replace part of a text file between markers with another text file?

Here is a Bash script using ed line editor to replace / update a part of multiline text in a text file with automatically generated content (eg: Table of contents) 👇

BEGIN GENERATED

  1. d5c10e45943b196b005771f1
  2. bc56de530fbf5342d55203b4
  3. 9608f31a09c2ece9c9cee4dd
  4. 54e361257aecce0a937211c0
  5. c4b2edf88293df0747694c37
  6. a80275accc9b2bc72d55efcd
  7. 3a2e2fec5da23b415569de00
  8. 524655319a05e7dd1f48e6a5
  9. 879d17e9e611d163e5a85b02
  10. 80d311a786efcd9d01da86f6

END GENERATED

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris et libero eu nunc dapibus volutpat. Proin eu neque eu orci volutpat bibendum...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment