1. Overview
Sometimes, we may want to add headers and footers to enhance or annotate text streams. Specifically, we can perform this task for several purposes, such as metadata inclusion, document categorization, and dynamic content insertion.
For instance, in log file analysis, we can append timestamps as a header which then helps in chronological organization. Moreover, we can add the system information as a footer to include the context or to provide additional information.
In this tutorial, we’ll check out methods to add a header and footer to a sed or an awk stream in Linux.
Specifically, we use the basic -e option of sed with the relevant commands. On the other hand, the -v option of awk and the BEGIN and END blocks in the AWK language can display basic and dynamic headers and footers along with the file content.
2. Using the sed Command
The sed or stream editor is a command-line tool used to parse and transform text streams. Moreover, we can use it to apply specific commands to the input lines, which enables versatile text manipulations, such as insertion, substitution, and deletion.
2.1. Using the -e Option
In this scenario, we add a basic header and footer to a sed stream with the -e option.
Let’s consider an example:
$ sed -e '1i Header Text' -e '$a Footer Text' test.txt
Header Text
This is line 1.
This is line 2.
This is line 3.
Footer Text
Now, let’s discuss the inner workings of the script:
- -e option enables the specification of multiple commands in a single line
- 1i inserts (i) Header Text before the first (1) line of the file
- $a appends (a) Footer Text after the last ($) line of the file
- test.txt is the input text file
As a result, the output displays Header Text at the beginning and Footer Text at the end of the file content.
2.2. Dynamic Header and Footer
We can also add a dynamic header and footer to a sed stream in a Linux terminal.
For instance, let’s add a header with the current date and a footer with the system hostname to a sed stream:
$ sed -e "1i Header: $(date)" -e "$ a Footer: $(hostname)" test.txt
Header: Tue 09 Jan 2024 10:45:02 AM EST
This is line 1.
This is line 2.
This is line 3.
Footer: linux
In the script above, sed with the -e option includes multiple commands in one line. Then, at the start of the test.txt file, 1i dynamically inserts $(date). This is the header that contains the current date.
Likewise, $a dynamically appends $(hostname) as a footer at the end of the file, which represents the current system hostname.
Consequently, we see the current date as a header and the system hostname as a footer in the sed stream.
3. Using the awk Command
In Unix-like operating systems, the awk command is used to search and process patterns within data streams or text files. Specifically, it enables us to create more complex text-processing tasks with its programming constructs, such as actions, patterns, and other variables.
3.1. BEGIN and END Blocks
In awk, we can use the BEGIN and END blocks to execute code before processing any lines and after processing all lines of a file.
For instance, we use the BEGIN and END blocks for adding a header and footer in a sed stream:
$ awk 'BEGIN {print "Header Text"} {print $0} END {print "Footer Text"}' test.txt
Header Text
This is line 1.
This is line 2.
This is line 3.
Footer Text
In the above script:
- BEGIN block executes once and prints Header Text before processing any lines from the file
- print $0 statement displays each line ($0) to the terminal
- END block executes once at the end and prints Footer Text after processing all lines of the file
- test.txt is the input text file
Thus, the output includes Header Text as a header, followed by the lines from the test.txt file, and the Footer Text as a footer.
3.2. Using the -v Option
We can also add the -v option to an awk stream to pass external variables for printing a dynamic header and footer.
Let’s consider an example:
$ awk -v header="Header: $(date)" -v footer="Footer: $(hostname)" 'BEGIN {print header} {print $0} END {print footer}' test.txt
Header: Tue 09 Jan 2024 10:51:54 AM EST
This is line 1.
This is line 2.
This is line 3.
Footer: linux
In the script above, the first -v option assigns $(date) or the current date to the header variable. Then, the second -v option assigns $(hostname) representing the system hostname to the footer variable.
Next, the BEGIN and END blocks work as before to print the dynamic header and footer along with the content of the test.txt file.
So, we get an equivalent output via awk as well.
4. Conclusion
In this article, we explored the methods to add a header and footer to a sed and an awk stream in Linux.
Naturally, we can use the -e option in sed to display a basic or dynamic header and footer via basic scripts. Furthermore, we can add the BEGIN and END blocks or the -v option to awk to display the header and footer before and after content.
Ultimately, we can use any of these effective methods based on the specific requirement of adding a header and footer to a sed or an awk stream.