« Back to Index

[Sed Insert Append + Prefix and Suffix]

View original Gist on GitHub

Tags: #sed #bash #insert #append #prefix #suffix

Sed Insert Append.bash

# Use `sed` to insert/append content around a match 
#
# Note: has to be specific version of Sed
#       I've found GNU Sed `gsed` works on my Mac but the system Sed doesn't

echo "foo bar baz" | gsed '/foo/i ---'
echo "foo bar baz" | gsed '/foo/a ---'

echo "foo bar baz" > words.txt
gsed '/foo/i ---' words
gsed '/foo/a ---' words

# Insert output
#
# ---
# foo bar baz

# Append output
#
# foo bar baz
# ---

$ echo -e "foo\nbar\nbaz" | sed 's/.*/PREFIX-&-SUFFIX/'
PREFIX-foo-SUFFIX
PREFIX-bar-SUFFIX
PREFIX-baz-SUFFIX