How To Use Stream Editor SED Command On FreeBSSD

Editing text files and terminal output is a daily job for those who manage FreeBSD machines. Command line utilities such as sed allow users to modify and change the contents of text files directly from the terminal window.

Sed (from English Stream Editor) is a console utility for editing text streams. This utility was originally developed for Unix, but was later cloned many times and ported to many platforms. Its cloning leads to the fact that in practice we can get completely unexpected results with absolutely identical calls, since clones do not follow the original in everything, and moreover they have differences among themselves. The difference can be avoided by simply using the rules described by POSIX.

Previously, text files were viewed or processed line by line, i.e., a user, working in a terminal, could not see the entire content, and line by line to make changes. On the other hand, the text can arrive at the terminal in several parts and the same effect is obtained. In all these cases, the incoming text forms a stream of characters. Using sed, users can find and replace specific words in text, view specific parts of the output, and edit text files without opening them.

The sed command allows us to define several processing rules for each line in the stream, and this is actually the main function of the sed command. The processing microprogram is compiled in the internal language sed, which declares special commands such as replace, delete, search and replace. By combining these commands, we can write very complex processing procedures that cover most of the needs encountered in practice. However, the sed command has the disadvantage that it is not very clear and sometimes complicated, and is not clear to uninitiated users.

The following are the jobs or tasks supported by the sed command:
  1. Output the source file in fragment form.
  2. Replace substrings in files using regular expressions.
  3. Deleting/Insert/Change source text fragments of a file according to some rules, in particular Sed is quite often used for automatic editing of system configuration files.
Advanced users can also implement regular expressions with the sed command to edit text streams more efficiently. In this article, we will discuss the sed command in detail, along with some important examples that demonstrate the power of the sed utility in FreeBSD.


1. Basic Syntax of sed Command

sed script [-Ealnru] [-i extension] [file ...]
sed [-Ealnu] [-i extension] [-e script] ... [-f script_file] ... [file ...]

The sed utility reads the specified file, or standard input if no file is specified, modifying the input as specified by the command list. The input is then written to standard output. A single command can be specified as the first argument to sed. Some commands can be specified using the -e or -f options. All commands are applied to input in the specified order regardless of their origin.

The following is a description of the options in the sed command.
  1. -E: Interpret regular expressions as extended (modern) regular expressions more often than regular expressionser dasar. Halaman dokumentasi di re_format yang menjelaskan kedua format secara lengkap.
  2. -a: Files listed as function parameters "w" are made enabled (or truncated) before the default process starts. The -a option causes sed to delay opening each file until a command containing the bound function "w" is applied to the input line.
  3. -e command: Adds the editing command specified by the command argument to the list in the command list.
  4. -f command_file: Added editing commands found in command_file to the command list. Each editing command must be listed on a separate line.
  5. -i extension: Editing files with replacement, saving backup copies with the specified extension. If the extension is not specified (zero length), the backup will not be saved. It is not recommended to set a zero extension on the files you replace because you risk corrupting the files completely or partially when disk space runs out.
  6. -n: by default, each line of input is output to standard output after all commands have been applied. The -n option overrides this behavior.

2. Regular Expression sed Command

The regular expressions used in sed are, by default, basic regular expressions (see re_format for more details), but extended (new) regular expressions can be used instead if the -E option is used. Additionally, sed has the following regular expression additions:
  1. In a context address, any character other than a backslash (“\”) or newline character can be used to delimit a regular expression. Additionally, using a backslash before a delimiter character forces that character to be a letter. For example, in the context address xabcxdefx, the delimiter character in the regular expression is "x", the second "x" is a letter, so the regular expression is "abcxdef".
  2. sequence, matches the newline character embedded in the pattern. You cannot use newline characters in address or substitution commands.
The only inherent feature of sed regular expressions is that they are, by default, relative to the last regular expression used. If the regular expression is empty, i.e. only the delimiter character is specified, then the last used regular expression is used. The last regular expression is defined as the last regular expression used, as part of an address or substitution command, at startup time, not at compile time. For example, the command "/abc/s//XXX/" will replace "XXX" with the pattern "abc".


3. How to Use sed Command

So that readers can be clearer about the function and use of the sed command, below we provide an example of its use. To make it easier, we will create a file called /tmp/employee.txt and enter the following lines.

root@ns1:~ # ee /tmp/employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager


a. Displays the contents of the file/script

Using the sed p command, you can print the current pattern space. You may be wondering why you need the p command, because by default sed prints the pattern buffer after executing its command. There's a reason for that, as you'll see; this command allows you to specifically control what is printed to stdout. Typically when p is used, you would use the -n option to suppress the default printing that occurs as part of the standard sed flow. Otherwise, when executing p (print) as one of the commands, the line will be printed twice. The following example prints each line of Employee.txt twice:
root@ns1:~ # sed 'p' /tmp/employee.txt
101,John Doe,CEO
101,John Doe,CEO
102,Jason Smith,IT Manager
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
104,Anand Ram,Developer
105,Jane Miller,Sales Manager
105,Jane Miller,Sales Manager

Now we will try to print each line once (the function is the same as 'cat Employee.txt').

root@ns1:~ # sed -n 'p' /tmp/employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

If an address range has not been specified before the sed command, by default it matches all lines. The following are some examples of specifying address ranges before the sed command. In the example below it will only display the second row.

root@ns1:~ # sed -n '2 p' /tmp/employee.txt
102,Jason Smith,IT Manager

If we only want to display the third row, then we can replace the number two above with the number 3.

root@ns1:~ # sed -n '1,4 p' /tmp/employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer

The command above will display lines 1 to 4. Then how can you display only lines 2 to 5, here are the commands.

root@ns1:/tmp # sed -n '2,$ p' employee.txt
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

We can also specify a numbered address (or range of addresses) and can also specify the pattern (or range of patterns) to match, as shown in the next few examples. The following is an example to display the contents of a file that has the character "Jane".

root@ns1:/tmp # sed -n '/Jane/ p' employee.txt
105,Jane Miller,Sales Manager

Another example is, we will display the character "Jason" up to the 4th line.

root@ns1:/tmp # sed -n '/Jason/,4 p' employee.txt
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer

If there are no matches for the character "Jason" in the first 4 lines, this command will print lines that match the character "Jason" after the 4th line. sed will display the first line "Raj" to the last line:

root@ns1:/tmp # sed -n '/Raj/,$ p' employee.txt
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

The sed command below will display lines with the character "Raj" through to lines that match "Jane".

root@ns1:/tmp # sed -n '/Raj/,/Jane/ p' employee.txt
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

We will practice the sed command again, this time we will use the sed command to display the line with the character "Jason" and the 2 lines after that.

root@ns1:/tmp # sed -n '/Jason/,+2 p' employee.txt
102,Jason Smith,IT Manager
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer


b. Delete Some Lines from File/Script

In point "b" we will use the sed command to delete lines. Please note that the line is simply removed from the output stream. Just like other sed commands, the d command does not change the contents of the original input file. By default, if we do not specify any address range before the sed command, it will match all lines. So, the following example will delete the second line from the employee.txt file and all the lines in the file will be displayed intact.
root@ns1:/tmp # sed '2 d' employee.txt
101,John Doe,CEO
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer
105,Jane Miller,Sales Manager

Now we will try to delete from rows 1 to 4.

root@ns1:/tmp # sed '1,4 d' employee.txt
105,Jane Miller,Sales Manager

In the example of the sed command above, 4 lines, namely lines 1 to 4 are not displayed and only line 5 is displayed.

root@ns1:/tmp # sed '2,$ d' employee.txt
101,John Doe,CEO

The sed command above will delete from line 2 to the last line, so only line 1 is displayed. Then we continue by deleting the line containing the character "Manager", here is an example.

root@ns1:/tmp # sed '/Manager/ d' employee.txt
101,John Doe,CEO
103,Raj Reddy,Sysadmin
104,Anand Ram,Developer

Delete lines starting from the first line with the character "Jason" to the 4th line.

root@ns1:/tmp # sed '/Jason/,4 d' employee.txt
101,John Doe,CEO
105,Jane Miller,Sales Manager

If there are no matches for the character "Jason" in the first 4 lines, this command only deletes the lines that match the word/character "Jason" after the 4th line. The command below will delete lines starting from the first line "Raj" to the last line.

root@ns1:/tmp # sed '/Raj/,$ d' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager
root@ns1:/tmp # sed '/Raj/,$ d' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager

Delete rows starting from the row that matches the word "Raj" to the row that matches the word "Jane".

root@ns1:/tmp # sed '/Raj/,/Jane/ d' employee.txt
101,John Doe,CEO
102,Jason Smith,IT Manager

Delete the lines starting from the line that matches "Jason" and the 2 lines after that word.

root@ns1:/tmp # sed '/Jason/,+2 d' employee.txt
101,John Doe,CEO
105,Jane Miller,Sales Manager

Another example of the delete command with sed. Now we will try another command from sed, namely, delete all empty lines from the employee.txt file.

root@ns1:/tmp # sed '/^$/ d' employee.txt

Remove all commented lines (assuming comments start with #).

root@ns1:/tmp # sed '/^#/ d' employee.txt

Most of the sed commands that have been exemplified above can also be run on almost all operating systems such as Linux/Solaris/AIX. So the AIX sed command/Linux sed command will also work the same way. Hope you liked this post about the sed command in FreBSD.
Iwan Setiawan

I Like Adventure: Mahameru Mount, Rinjani Mount I Like Writer FreeBSD

Post a Comment

Previous Post Next Post