sed command is a Stream Editor which is used for modifying the files in Linux. Sed helps to make changes to the file.
Example
This file can be taken as an example file for using this command.
This file can be taken as an example file for using this command.
To replace the word 'Unix' with 'Linux'
Options
s – substitution operation
/ - delimiters
Default: It will replace the first occurrence in the line only.
Replacing the specific occurrence in a line
To replace the second occurrence of 'Unix' with 'Linux' in a line.
Options
/2 – to notify the second occurrence
Replacing the entire occurrence in a line
To replace 'Unix' with 'Linux' for all occurrence in a line.
Options
/g – global replacement
Replacing from the specific occurrence to all
To replace all 'Unix' with ' Linux' from the given occurrence to the entire occurrence following back.
Options
/3g – to notify the change to be made from third occurrence.
/3g – to notify the change to be made from third occurrence.
Using '&' flag to mention string
There may be need to replace the pattern by adding some characters to it. To find the specific word that need to make change, this '&' is used.
Duplicates the replaced line
If the change is made then the line will be displayed twice in terminal. If not, it remains the same.
Options
/p – print flag to display twice.
Printing only replaced line
If the pattern is altered then the specific line will be displayed alone.
Options
-n with /p – to display twice the altered line alone.
Nested sed
Running multiple sed conditions in a single command, i.e., taking the output of first instance as the input for second instance and so on.
Another method
Options
e – To run multiple sed commands.
Replacing a word in specified line number
The command can be made to execute only in the specific line number.
Options
3 – it mentions the line number.
Replacing words in the range of lines
You can specify a range of line numbers to execute the command.
Options
1, 3 – it mentions the range of line numbers.
Another method
Replace if pattern matches
Specify a pattern in sed command. If pattern match occurs then sed command looks for the string to be replaced.
Here, it looks for the pattern Linux and then replace the word Unix with centos.
Deleting lines
You can delete the lines of a file by specifying line number or a range.
To delete the range of lines
Options:
d - Delete
$ - to indicate last line
Adding a new line after a match
sed command can add a new line after a pattern match is found.
Options
a – to add a new line after a match is found.
Adding a new line before a match
It can add a new line before a pattern is found.
Options
i – To add a new line before a match is found.
Replacing entire line
sed command can be used to replace the entire line with a new line.
Options
c – To notify change of line.
Case-sensitive option
sed command can be used to convert lower case to upper case letters.
Options
y – to transform the case of letters.
rh – letters to change from lower case to upper case in a file.
RH – to notify the upper-case option.
y – to transform the case of letters.
rh – letters to change from lower case to upper case in a file.
RH – to notify the upper-case option.
0 comments:
Post a Comment