Sed in UNIX: File Modification at the Tips of Your Fingers

Html Web CodeIn UNIX, regular expressions are strings that can be utilized for describing characters in several sequences. Several different UNIX commands use regular expressions including grep, awk, sed, ed and also vi. Here we are going to talk about Sed in UNIX.

Basically, sed in UNIX is a special editor for automatically modifying files. When you want to create a program that can alter a file, the tool to use is sed.

In the UNIX toolbox, sed is a real workhorse. Even with its rich set of commands that perform various actions, sed is a simple-to-use program for different applications, which you can learn more about by taking this UNIX course.

The S Command

There are many commands in sed but many people only learn the ‘S’ substitute command. This command causes a change in every occurrence of a new value’s regular expression. For example, you can change an old file word ‘day’ into the word ‘night’ for the new file.

For UNIX beginners, you can use:

sed 1

You can also change ‘day’ to ‘night’ with:

sed2

If you want to test the output, which should be ‘night,’ type:

pic3

Keep in mind that sed is line-oriented, so if you have a file input:

Four five six, four five six

Seven six five four

Four thousand

and you type the command:

sed4

You would get this output:

FOUR five six, FOUR five six

Seven six five FOUR

FOUR thousand

Look how this changed ‘four’ to ‘FOUR’ on ever line. The first line had ‘one’ 2x, but only the 1st occurrence was altered. This is actually the default behavior. You will need to use other available options if you want something different.

The substitute command has four parts. On the left hand side you have a search pattern, and on the right hand side that is the replacement string.

ONE       Replacement string

One       Regular Expression Pattern Search Pattern

/../../     Delimiter

S              substitute command

Using & as the Matched String

You may want to add some characters like parenthesis and search for a pattern near or around the pattern you found. When you are looking for a particular string, it easy to do this:

sed5

In the replacement string, you can have any number of ‘&.’ You can also double the 1st number of a line or any pattern:

sed7

Using Slashes as Delimiters

The delimiter is the character after the ‘S.’ Conventionally, it is a slash. When you want to alter slash names containing slashes, such as in say /usr /local /bin, to quote the slash, you can use the backslash:

sed9

Some people use colons:

sed11

Other people use the character “|”

sed12

When you use underlines instead of slashes, this could be easier to read:

sed10

Ranges of Text and Addresses

In sed, the substitute command is treating each line without really altering any of the other lines. The ability to restrict operations to just specific lines is quite useful, and you can use the following restrictions:

All lines between 2 regular expressions

All lines from the regular expression to the file end

All lines from the start of the file to a regular expression

All lines that contain patterns

Using numbers to specify a range of lines

Using its number to specify a line.

Write to File with /w Filename

With /w, you can specify files that get the modified data. This example writes all the lines beginning with even numbers and followed by 1 space to the ‘even’ file:

sed14

Since the input was not modified, the output file is not needed in this example. You need to have exactly 1 space between the filename and the ‘w.’ You can also have 10 files open, which allows you to split up data streams into separate files. Depending on the first number’s last digit, you can split the file into 10 pieces. This method can also be used for debugging information or logging errors to special files.

/p Print

Sed prints every line by default. The new text is printed instead of the old one if it makes a substitution. If you use optional arguments like “sed-n,” this won’t result in printing a new line. When the option “-n” is used, the flag “p” causes the modified line to get printed out. You can duplicate the grep function in sed with:

sed13

/g Global Replacement

Many utilities of UNIX work on files, reading line per line, which you can read more about in this UNIX article. By default, sed is the same way. If you tell sed to change words, it will only alter the first time a word occurs on a line. Instead of just the first word, you may want to change every word in the line.

One example is placing parentheses around the line words. Rather than using a “[A-Za-z]* which will not match words such as don’t, you can use a “[^]* pattern, which matches all words and now spaces. Remember that the asterisk means ‘0’ or ‘more.’

Sed UNIX Multi Line Patterns

Many UNIX utilities are line oriented, including regular expressions. It is not an easy task to search for patterns the cover more than 1 line.

In a line of text, sed reads from the file input and puts it in the pattern spaces. If the file end is found and if there are extra files you can read, the next file is opened as the current file is closed. The new file’s first line is placed into the pattern spaces.

The line count gets increments of one and opening new files do not reset this numeral.

There is then an examination of each sed command. If restrictions are placed on any of the commands and the current pattern space’s line meets the restrictions, there is an execution of the command. Some commands like‘d’ or ‘n’ result in sed going to the top of the loop. The command ‘q’ causes sed to stop. Otherwise, there is an examination of the next command. Once every command is examined, the pattern spaces become the output unless sed has the ‘n’ argument, which is optional. For more on computer languages, you might want to take this Linux course or even this one, entitled Comptia Linux.