Here are a few suggestions and hints to automating report edits from the Linux command line.
Search and update textual content in a report on Linux
sed, you need to use a regular expression. A regular expression is a set of special characters that define a pattern. My most frequent example of using
sed is replacing text in a file. The syntax for replacing text looks like this:
s tells sed to perform text replacement or swap occurrences of text. Provide the original text and new text between slashes.
This syntax will only replace the first occurrence of
originaltext on each line. To replace every occurrence, even if the original text appears more than once on a line, append
g to the end of the expression. Here is an example:
To use this with
sed, specify this regular expression with the
For example, let's say I have a Makefile for a program called game, which simulates Conway's Game of Life:
The name game isn't very descriptive, so I might choose to rename it life. Renaming the
game.c source file to
life.c is easy enough, but now I need to modify the Makefile to use the new name. I can use
sed to change every occurrence of game to life:
This prints the
sed output to the screen, which is a good way to check if the text replacement will do what you want. To make these changes to the Makefile, first, make a backup of the file, then run
sed and save the output to the original filename:
If you are confident that your changes are exactly what you want, use the
--in-place option to edit the file in place. However, I recommend adding a backup filename suffix like
--in-place=.old to save a copy of the original file in case you need to restore it later. It looks like this:
Quoting documents with sed on LinuxYou can use different capabilities of everyday expressions to in shape particular times of textual content. For instance, you would possibly want to update textual content that happens on the begin of a line. With sed, you may in shape the start of a line with ^, the caret character.
One way I use "start of line" in replacing text is when I need to quote a file in an email. Let's say I want to share my Makefile in an email, but I don't want to include it as a file attachment. Instead, I prefer to "quote" the file in the body of an email, using > before each line. I can use the following
sed command to print out an edited version to my terminal, which I can copy and paste into a new email:
s/^/>/ regular expression matches the start of each line (^) and places a > there. Effectively, this starts each line with the > symbol.
The tabs might not show up correctly in an email, but I can replace all tabs in the Makefile with a few spaces by adding another regular expression:
\t indicates a literal tab, so
s/\t/ /g tells sed to replace all tabs in the input with two spaces in the output.
If you need to apply lots of edits to files, you can save your
-e commands in a file and use
-f to tell
sed to use that file as a "script." This approach is especially useful if you need to make the same edits frequently. I might have prepared the Makefile for quoting in email using a script file called
Learn to work with sed on Linux
sed is a great tool to keep in your Linux command-line toolkit. Explore the
sed manual page and learn more about how to use it. Type
man sed at the command line to get complete documentation about the different command line options and how to use
sed to process text files.