Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace a whole line with sed?

Tags:

regex

sed

People also ask

How do you sed a line?

By default, the sed command replaces the first occurrence of the pattern in each line and it won't replace the second, third… occurrence in the line. Replacing the nth occurrence of a pattern in a line : Use the /1, /2 etc flags to replace the first, second occurrence of a pattern in a line.

Which sed command is used for replacement?

The s command (as in substitute) is probably the most important in sed and has a lot of different options. The syntax of the s command is ' s/ regexp / replacement / flags '.


Try this:

sed "s/aaa=.*/aaa=xxx/g"

You can also use sed's change line to accomplish this:

sed -i "/aaa=/c\aaa=xxx" your_file_here

This will go through and find any lines that pass the aaa= test, which means that the line contains the letters aaa=. Then it replaces the entire line with aaa=xxx. You can add a ^ at the beginning of the test to make sure you only get the lines that start with aaa= but that's up to you.


Like this:

sed 's/aaa=.*/aaa=xxx/'

If you want to guarantee that the aaa= is at the start of the line, make it:

sed 's/^aaa=.*/aaa=xxx/'

sed -i.bak 's/\(aaa=\).*/\1"xxx"/g' your_file

If you would like to use awk then this would work too

awk -F= '{$2="xxx";print}' OFS="\=" filename