Linux sed command example
Now we are going to try some examples. All examples are also considered as tutorials. When needed we have explained the command of given examples.
Sed basic syntax
|
sed 'pattern' file
sed -e 'pattern' file
sed -n 'pattern' file
|
- First command used very simply. To search we just need to give it an string.
- -e options can be used for multiple pattern
- -n Does not output in the terminal. Wait for print command.
First Example of sed
|
sed 's/pcspeech/pc speech/' reg
#Output
This is enough for regex.
pc speech pcspeech pcspeech
|
This command finding pcspeech word and replacing with pc speech.
- sed (the built-in command).
- The pattern enclosed within single quote.
- s for substitute the line.
- Delimited the substitution with slash.
- The target word or characters or even sentence.
- Delimited the searching word again.
- The word to be replaced and delimited.
- The target file.
Replacing Globally
Sed find the pattern for once in every line. But it does not replace the every found string(see previous example). In this case we need to give it address range. For example, Globally to replace all found strings.
|
sed 's/pcspeech/pc speech/g' reg
#output
This is enough for regex.
pc speech pc speech pc speech
|
Delete the lines using sed command
If the pattern found then delete the lines(goes to next line automatically. We can do this with d ).
Multiple Commands in sed
We can run multiple commands at a time in two way. 1) Terminating the command with semicolon 2)With option -e.
|
sed 's/pcspeech/pc speech/g; s/1337/ninja/' reg
|
|
sed -e 's/pcspeech/pc speech/g
> /1337/d
> s/computer/pc/' reg
|
Only look at specific lines
|
sed '3s/pcspeech/pc speech/g' reg
sed '3,10s/pcspeech/pc speech/g' reg
|
First command only search pattern at line 3.
Second Command the search start from line 3 and end at line 10 then replace the string.
Removing blank lines with sed
If we have unnecessary blank lines then we can remove these lines easily with sed. Here is an example
Don’t print if found
This is called negating(!) command in sed. Normally sed print all lines but when we negate the sed command this will ignore the line.
|
sed -n '/pcspeech/!p' reg
|
If the pcspeech found in any line of the text file reg.txt then the line would not printed as we have negated it with !p.
Sed command Branching
|
sed -n '{/pcspeech/b mybranch;
:mybranch
/guys/p}' reg
|
sed Recalling
|
echo "good is mood" | sed 's/.od/"&"/g'
|
This command matching anything before before the dot. This will match aaas,aod,aood etc. Also we are recalling the expression with &.
Saving the modified data to a file
|
sed 's/pcspeech/pc speech/gw reg1.txt' reg
|
This command replacing the word globally(g) and w used to tell that we want to save it toreg1.txt file.
Using different delimiter
We mostly seen the delimiter is forward slash. Sometime forward slash make the pattern hard to read the command if we escaped any other special characters using backslash. The easiest solutions for this :
So if we want to do something with /etc/passwd, for example changing the default shell then it would be like this
|
sed 's!/bin/bash!/seen/ash!' /etc/passwd
|
[padding right=”0%” left=”5%”]
How it is working
- s for substitution.
- ! used for alternative delimiter whereas mostly used forward slash as delimiter. We used ! to make it more readable since backslash with forward slash will make it harder to read.
- The default shell we want to replace is /bin/bash
- Delimited with ! .
- The replaced string(shell) is /seen/ash
- Last delimitation with ! .
- The target file /etc/passwd
[/padding]
If we want to do same thing in hard way , then it will be look like this:
|
sed 's/\/bin\/bash/\/seen\/ash/' /etc/passwd
|
This command performing same task but it looks more horrible!
More delete with Linux sed command
Delete the first line
Delete line 1-3 of lines
Delete the line if found the search pattern
Append/Insert/Read with sed command
Append a line at the end
|
sed '$a\Something different' reg
|
Usually append command append a line after the data stream if we don’t specify an address. For example
|
echo "What the" | sed 'a\hell'
|
We can also append a line after specific line number
|
sed '2a\Something different' reg
|
Insert command insert the line before all line. For example
|
echo "What the" | sed 'i\hell'
|
|
echo -e "I am pcspeech\nwho are you?\nwhat the hell" | sed 'i\inserted line'
#output
inserted line
I am pcspeech
inserted line
who are you?
inserted line
what the hell
|
Better idea is to specify an address
|
echo -e "I am pcspeech\nwho are you?\nwhat the hell" | sed '1i\inserted line'
echo -e "I am pcspeech\nwho are you?\nwhat the hell" | sed '$i\inserted line'
|
Read from file
See the Last line
What if you want to print only last line?
Cleaning up html tags from a file
|
sed 's/<[^>]*>//g ; /^$/d'
|