Quantcast
Channel: GoLinuxHub
Viewing all articles
Browse latest Browse all 392

sed :Replace whole line when match found

$
0
0
Here our requirement is to replace a line with our content when a match is found with in the file



Our sample file /tmp/file
This is line one
This is line two
This is line three
This is line four

Here we will search for line having the string "two" and replace the complete line with "your text"
# sed '/two/c\your text' /tmp/file

This is line one
your text
This is line three
This is line four

To do in place replacement in the same file
# sed -i '/two/c\your text' /tmp/file
Similarly if you want to match the line having the string "three" and replace the line with "your text"
# sed '/three/c\your text' /tmp/file
This is line one
This is line two
your text
This is line four

To do in place replacement in the same file
# sed -i '/three/c\your text' /tmp/file

IMPORTANT NOTE:
Do not use this unless you are very sure the command will not impact anything else, it is always recommended to take a backup of such file where you plan to do in place replacement

Follow the below links for more tutorials

How to find the path of any command in Linux
How to configure a Clustered Samba share using ctdb in Red Hat Cluster
How to delete an iscsi-target from openfiler and Linux
How to perform a local ssh port forwarding in Linux
How to use yum locally without internet connection using cache?
What is umask and how to change the default value permanently?
Understanding Partition Scheme MBR vs GPT
How does a successful or failed login process works in Linux
How to find all the process accessing a file in Linux
How to exclude multiple directories from du command in Linux
How to configure autofs in Linux and what are its advantages?
How to resize software raid partition in Linux
How to configure Software RAID 1 mirroring in Linux
How to prevent a command from getting stored in history in Linux

Viewing all articles
Browse latest Browse all 392

Latest Images

Trending Articles



Latest Images