The identify sed stands for “Motion Editor,” and it’s an outstanding tool that allows you to parse and change into text correct from the command line. Whether or not or now not you’re dealing with configuration files, scripts, or even easy text, sed
is your go-to device for fast and surroundings pleasant text manipulation.
The main use of sed
is to search for particular patterns of text and replace them with something else. It will moreover delete or insert lines and perform other text transformations. It’s particularly useful for batch editing of files or for working inside shell scripts to automate various tasks.
While sed is very versatile on its own, it’s often used in aggregate with other Linux directions like awk
for text processing, grep
for construction taking a look out, and cat
for appearing document content material subject matter. Together, the ones equipment form a powerful toolkit for text processing inside the Linux surroundings.
Commonplace syntax for sed
command:
$ sed [OPTIONS] [FILE]...
1. Text substitution
echo "Text" | sed 's/Replaceable_Word/The_Word_That_Replaces/'
Use the sed
command to seem and replace any part of the text. 's'
indicates a search and replace process.
Example:
Let’s say you’ve were given a string “I in point of fact like CSS
” and you want to exchange “CSS” with “CSS Libraries
“.
echo "I in point of fact like CSS" | sed 's/CSS/CSS Libraries/'
I in point of fact like CSS Libraries
In this example, the echo command outputs “I in point of fact like CSS
“, and then sed replaces “CSS
” with “CSS Libraries
“. The total output is “I in point of fact like CSS Libraries
“.
2. Trade text on a selected line in a document
sed '[line] s/harder/more straightforward/g' [file]
The 'g'
collection of the sed
command is used to exchange the rest that matches the improvement.
Example:
Let’s say you’ve were given a text document named example.txt
with the following content material subject matter:
Lifestyles is difficult. Running harder is the necessary factor to just right fortune. The harder you are hired, the luckier you get.
To replace all occurrences of the word “harder
” with “more straightforward
” on line 2 of example.txt
, you may be able to run:
sed '2 s/harder/more straightforward/g' example.txt
After working the command, the output displayed on the terminal may well be:
Lifestyles is difficult. Running more straightforward is the necessary factor to just right fortune. The harder you are hired, the luckier you get.
Realize that the word “harder
” is modified with “more straightforward
” most efficient on line 2.
If you want to save the ones changes once more to the document, you’ll use the -i
selection:
sed -i '2 s/harder/more straightforward/g' example.txt
After working this command, the content material subject matter of example.txt
may well be utterly changed to:
Lifestyles is difficult. Running more straightforward is the necessary factor to just right fortune. The harder you are hired, the luckier you get.
3. Trade first matching with new text
sed 's/harder/more straightforward/' [file]
This command replaces most efficient the principle are compatible of the search construction.
Example:
Let’s say you’ve were given a text document named example.txt
with the following content material subject matter:
Lifestyles is more challenging than we think. Running harder is the necessary factor to just right fortune. No pain, no achieve. Art work harder.
You’ll use the sed command to exchange the word “harder
” with “more straightforward
” in each line:
sed 's/harder/more straightforward/' example.txt
After working the command, the output may well be:
Lifestyles is more straightforward than we think. Running more straightforward is the necessary factor to just right fortune. No pain, no achieve. Art work more straightforward.
4. Remove matching lines
sed '/Something/d' example.txt
Use the d
collection of the sed
command to remove any line from a document.
Example:
Let’s say you’ve were given a document known as example.txt
with the following content material subject matter:
Hello Global Something is true right here Every other line However another line Something else
Operating the command sed '/Something/d' example.txt
will output:
Hello Global Every other line However another line
5. Search for a case-insensitive word + delete it
sed '/Development/Id' example.txt
The I
collection of the sed
command is used to search for an an identical construction in a case insensitive means.
Example:
Let’s say you’ve were given a document named example.txt
with the following content material subject matter:
This can be a Development line. Every other line. However another Development line. Final line.
Operating the command sed '/Development/Id' example.txt
will produce the following output:
Every other line. Final line.
6. Trade words with uppercase
sed 's/(libraries)/U1/Ig' example.txt
Use the U
collection of the sed
command to turn into any text to uppercase letters.
Example:
Let’s say you’ve were given a document named example.txt
with the following content material subject matter:
I in point of fact like libraries. libraries are great. You'll be able to to find many books in libraries.
After working the sed
command, the output may well be:
I in point of fact like LIBRARIES. LIBRARIES are great. You'll be able to to find many books in LIBRARIES.
7. Trade words with lowercase
sed 's/(libraries)/L1/Ig' example.txt
The L
collection of the sed
command is used to turn into any text to lowercase letters.
Example:
Let’s say you’ve were given a document named example.txt
with the following content material subject matter:
Libraries are an important for research. libraries lend a hand in many ways. I in point of fact like LIBRARIES!
After working the sed
command, the output may well be:
libraries are an important for research. libraries lend a hand in many ways. I in point of fact like libraries!
8. Insert blank lines in a document
sed G [file]
Use the G
collection of the sed
command to insert blank lines after each line of the document.
Example:
Let’s say you’ve were given a document known as example.txt
with the following content material subject matter:
Hello Global This Is A Check out
You’ll run the following command to append an extra newline at the end of each line:
sed G example.txt
After working the command, the output may well be:
Hello Global This Is A Check out
9. Print document’s line numbers
sed '=' [file]
The =
sign is used to print a line amount forward of each line of text in a document.
Example:
Let’s say you’ve were given a document named example.txt
with the following content material subject matter:
Hello Global This Is A Check out
You’ll run the following command to print the street numbers forward of each line:
sed '=' example.txt
1 Hello 2 Global 3 This 4 Is 5 A 6 Check out
Further Linux directions:
Checklist Operations | rmdir · cd · pwd · exa · ls |
Record Operations | cat · cp · dd · much less · contact · ln · rename · extra · head |
Record Software Operations | chown · mkfs · find |
Networking | ping · curl · wget · iptables · mtr |
Search and Text Processing | in finding · grep · sed · whatis · ripgrep · fd · tldr |
Software Wisdom and Keep an eye on | env · historical past · best · who · htop · glances · lsof |
Particular person and Session Keep an eye on | display screen · su · sudo · open |
The post The best way to Use the sed Command in Linux gave the impression first on Hongkiat.
Supply: https://www.hongkiat.com/blog/linux-command-sed/
Contents
- 0.0.0.1 1. Text substitution
- 0.0.0.2 2. Trade text on a selected line in a document
- 0.0.0.3 3. Trade first matching with new text
- 0.0.0.4 4. Remove matching lines
- 0.0.0.5 5. Search for a case-insensitive word + delete it
- 0.0.0.6 6. Trade words with uppercase
- 0.0.0.7 7. Trade words with lowercase
- 0.0.0.8 8. Insert blank lines in a document
- 0.0.0.9 9. Print document’s line numbers
- 0.0.0.10 Further Linux directions:
- 0.1 Related posts:
- 1 Easy methods to Repair ’ERR_SSL_VERSION_OR _CIPHER_MISMATCH’ in WordPress
- 2 Find out how to Get right of entry to Blocked Web pages (9 Techniques)
- 3 AI Content material Turbines: I Examined 5 of the Highest, and This is What I Discovered
0 Comments