Introduction
Set is an indispensable tool in the arsenal of any system manager or developer working in Linux environments. Its name means' stream editor 'and its main function is to process text flows in a linear way, applying transformations without the need to load the full file into memory. This makes it extremely efficient for large files or for using inside pipes and scripts. In this article we will explore from the basic syntax to advanced examples that will allow you to make the most of the power of thirst.
What's thirsty?
Although thirst may seem cryptic at first sight, its logic is simple: read entry line by line, run the instructions we provide and send the result to the standard output. Do not modify the original file unless we explicitly indicate you with options like -i. This feature makes it a safe ally for tests and prototypes, as we can experience without risk of damaging data.
Basic syntax
The simplest way to invoke thirst is: thirst 'command' file. The command goes between simple quotes to prevent the shell from interpreting special characters. The most frequent commands start with a direction that specifies which lines affect, followed by action. For example, '5,10s / foo / bar /' replaces foo by bar only in the lines 5 to 10. If the direction is omitted, the action applies to all lines.
Most common operations
- Replacement: the order s / pattern / replacement / flags changes the first pattern match in each line. By adding the g flag all coincidences are replaced.
- Elimination: the order d erases lines that match the given direction. For example, '/ ^ # / d' eliminates all lines that start with the pad symbol.
- Insertion: with i text text is inserted before the selected line, while to text it is added later.
- Printing: the p command shows lines that meet a condition, useful when combined with the -n option to suppress the automatic output.
Practical examples
- Change the extension of several files to a list: | thirst's / .txt$/ .md / '
- Remove comments from a shell script: thirsty '/ ^ # / d'script.sh > clean script _. sh
- Add line numbering to a file: thirsty '=' file | thirst 'N; s / n / t /'
- Convert mail addresses to small letters: thirst's /.*/ L & / 'entrada.txt
Advanced use
Set allows to chain multiple expressions using -e or simply by separating them with point and coma within a single quotation. For example, thirst -e's / foo / bar / '-e's / baz / qux /' file applies both replacements. It also has two work spaces: the space pattern and the hold space. With the orders h, H, g, G and x we can move data between them, allowing for transformations that depend on previous or subsequent lines, such as double lines or create summaries.
Another powerful feature is the ability to read command scripts from a file with -f script.sed, which facilitates the reuse of complex routines in different projects.
Tips and good practices
- Always try your commands with a backup or using the standard output before applying -i.
- Use simple quotes around the thirst script to prevent the shell from expanding variables or interpreting inverted bars.
- When you need to include a diagonal bar in the replacement pattern, consider using a different delimiter, for example, s|route / old|route / new|.
- Take advantage of the -n option along with p to print only what you really need, reducing the noise in the output.
- Document your thirst scripts with comments within the .sed file, starting each line with #.
Conclusion
The thirst command remains a key piece for anyone working with Linux text. Its ability to process flows quickly, its flexible syntax and low resource consumption make it ideal for simple replacement tasks and for more elaborate transformation scripts. To know thirst not only saves time, but also opens the door to efficient process automation that would otherwise require more complex programming. We invite you to practice the examples shown and explore your manual to discover even more tricks that will make your daily work more productive.


