The best way to Use the sed Command in Linux

by | Nov 9, 2023 | Etcetera | 0 comments

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.

See also  9 Perfect Exercise Apps for a Are compatible 2023

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.

See also  Learn how to Promote Virtual Artwork and Graphics On-line (The Simple Approach)

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.

See also  WP Engine Complements Its Company Spouse Program With Precedence Give a boost to, Business-Main Incentives, and Further Expansion Advantages

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.

WordPress Website Development

Supply: https://www.hongkiat.com/blog/linux-command-sed/

[ continue ]

WordPress Maintenance Plans | WordPress Hosting

read more

0 Comments

Submit a Comment

DON'T LET YOUR WEBSITE GET DESTROYED BY HACKERS!

Get your FREE copy of our Cyber Security for WordPress® whitepaper.

You'll also get exclusive access to discounts that are only found at the bottom of our WP CyberSec whitepaper.

You have Successfully Subscribed!