How to Use Sed
Ads by Google
SED stands for stream editor. You can use it in Linux or UNIX and as a command line terminal or you can run your Linux command through SED. You can used it for text substitution for finding and replacing. SED also perform a lot of manipulations on the text like searching, deletion, and insertion. You can edit the file using SED without opening the files. SED also provides a better way for using the arithmetic operation and their manipulation.
In this article we will discuss about the use of SED. How to use the GNU SED with different commands and their examples.
An Overview of SED
There are three steps in SED for performing a task:
- Red data or command from input stream
- Execute and run this commands
- Show the results on output stream
Options in SED
With SED commands you can use different options that perform some specific tasks against that commands:
“ –i ” This option helps to edit the source files
“ –e ” If you want to run multiple SED commands with a single command than –e is very useful.
“ –n ” This option used for default output.
“ –f ” This option help to run a SED script files.
SED commands with Special Characters
SED also provide some special characters. You can use these characters for different purposes. Some special characters and their description is given below:
“ ^ ” This character helps to find the beginning of the line.
“ $ ” The dollar sign helps for finding the end of the line.
“ . ” Dot sign find the single characters from the text.
“ * ” The steric sign find the occurrences of previous character.
How to use SED with Commands and their Examples
Similar to Linux commands SED has its own commands and there is a lot of commands are available in SED. Now we are focusing on different SED Commands with examples:
Get the Lines from File with Given Range
# sed -n ‘ 8, 12p ‘ filename.txt
The above command return the lines from 8 to 12 from filename.txt.
Get All Lines except a given Range
# sed ‘ 15, 25d ‘ filename.txt
This command will return all the text from filename except lines from 15 to 25.
The use of “-e” option
# sed -n -e ‘ 4, 8p ‘ -e ‘ 12, 16p’ filename.txt
This command will the lines from 4 to 8 and also return from 12 to 16 from whole text in filename.txt.
Insert Blank Spaces in Files
SED provides a command for inserting blank spaces in file text.
# sed G filename.txt
Search data and Replace it
You can find some text or data from file in SED and replace that data using command:
# sed ‘ s/John/William/g’ filename.txt
In this command first SED will find the John and then replace it with William in all over the file.
Add a complete Line After or before
# sed ‘/John/a ‘ Add this new line after matching ‘ ‘ filename.txt
This command work similar above command first search keyword then add given new line after that keyword.
What is the use of sed command?
How do I run a sed script?
- you can give the commands on the command line: sed ‘s/yes/done/’ file. If you want to use several commands, you have to use the -e option:
- it is also possible to place the commands in a seperate file: sed -f sedsrc file. where sedsrc contains the necessary commands.
How do you insert a line using sed?
- Insert line using the Line number. This will insert the line before the line at line number ‘N’. Syntax: sed ‘N i <LINE-TO-BE-ADDED>’ FILE.txt Example:
- Insert lines using Regular expression. This will insert the line before every line where pattern match is found. Syntax:
How do you use SED in Awk?
- BEGIN{FS=OFS=” : “} use : as input/output field separator.
- gsub(/ /,”_”,$2) replace all spaces with _ only for second field.
- Similarly other substitutions as required.
- 1 at end of command is idiomatic way to print the line, includes any changes made.
- See also awk save modifications in place.
How do I replace text in awk?
Can we use awk inside awk?
What awk command does?
What does AWK mean?
What is difference between sed and awk?
How do I write an awk script?
Explaining the line above:
- #! – referred to as Shebang, which specifies an interpreter for the instructions in a script.
- /usr/bin/awk – is the interpreter.
- -f – interpreter option, used to read a program file.
What does $0 represent in awk?
What does print $0 mean?
What is awk and sed?
What is a $1 awk?
What AWK $2?
What is print $1?
How do you sum in awk?
Ads by Google