Complete Guide to Adding Strings After Each Line in Files Using sed Command in Bash

Nov 26, 2025 · Programming · 12 views · 7.8

Keywords: Bash | sed command | file processing | text editing | Linux system administration

Abstract: This article provides a comprehensive exploration of various methods to append strings after each line in files using the sed command in Bash environments. It begins with an introduction to the basic syntax and principles of the sed command, focusing on the technical details of in-place editing using the -i parameter, including compatibility issues across different sed versions. For environments that do not support the -i parameter, the article offers a complete solution using temporary files, detailing the usage of the mktemp command and the preservation of file permissions. Additionally, the article compares implementation approaches using other text processing tools like awk and ed, analyzing the advantages, disadvantages, and applicable scenarios of each method. Through complete code examples and in-depth technical analysis, this article serves as a practical reference for system administrators and developers in file processing tasks.

Basic Principles of the sed Command

sed (Stream EDitor) is a powerful stream editor in Unix/Linux systems, specifically designed for non-interactive editing of text data. In the scenario of appending strings after each line in a file, sed achieves text appending by matching the end-of-line position using regular expressions.

The core syntax structure of the sed command is: sed -e 's/$/string to append/' filename. Here, s denotes the substitution operation, and $ is the end-of-line anchor in regular expressions. This command replaces the end position of each line (an empty string) with the specified string, thereby achieving the appending effect.

In-Place Editing Using the -i Parameter

Modern versions of sed typically support the -i parameter, which allows direct modification of the original file without creating temporary files. The complete command format is: sed -e 's/$/string after each line/' -i filename.

It is important to note that the behavior of the -i parameter may vary across different operating systems and sed versions. GNU sed generally fully supports this feature, while BSD sed (such as the version in macOS systems) may require specifying a backup file suffix, for example: sed -i '.bak' 's/$/string/' filename.

Temporary File Solution

For sed environments that do not support the -i parameter, a temporary file approach must be used to achieve safe editing:

typeset TMP_FILE=$(mktemp)
touch "${TMP_FILE}"
cp -p filename "${TMP_FILE}"
sed -e 's/$/string after each line/' "${TMP_FILE}" > filename

Key technical points of this solution include:

Comparative Analysis with Other Tools

In addition to sed, similar functionality can be achieved using tools like awk and ed:

awk implementation: awk '1;/PATTERN/{ print "add one line"; print "\\and one more"}' infile. awk offers advantages in handling complex logic but has a relatively more complex syntax.

ed editor, as a standard text editor, provides comprehensive line editing capabilities:

ex -s infile <<\IN
/PATTERN/a
add one line
and one more
.
w
q
IN

Although the ed command has an archaic syntax, it ensures consistency across all Unix-like systems.

Escape Character Handling

During text processing, special characters need to be properly escaped. For example, to output a literal backslash, \\ must be used; in multi-line appends with sed, each newline must be preceded by a backslash:

sed '/PATTERN/a\
add one line\
\\and one more' infile

Security Considerations

When using file editing commands in actual production environments, the following security factors should be considered:

Performance Optimization Suggestions

For large file processing, the following optimization measures can be taken:

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.