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
1
2
3
|
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
1
2
3
4
5
|
sed 's/pcspeech/pc speech/' reg
#Output
This is enough for regex.
pc speech pcspeech pcspeech
|
- 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.
1
2
3
4
5
|
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 ).
1
|
sed '/pcspeech/d' reg
|
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.
1
|
sed 's/pcspeech/pc speech/g; s/1337/ninja/' reg
|
1
2
3
|
sed -e 's/pcspeech/pc speech/g
> /1337/d
> s/computer/pc/' reg
|
Only look at specific lines
1
2
|
sed '3s/pcspeech/pc speech/g' reg
sed '3,10s/pcspeech/pc speech/g' reg
|
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
1
|
sed '/^$/d' reg
|
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.
1
|
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
1
2
3
|
sed -n '{/pcspeech/b mybranch;
:mybranch
/guys/p}' reg
|
sed Recalling
1
|
echo "good is mood" | sed 's/.od/"&"/g'
|
Saving the modified data to a file
1
|
sed 's/pcspeech/pc speech/gw reg1.txt' reg
|
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 :
1
|
sed 's!string!string!
|
So if we want to do something with /etc/passwd, for example changing the default shell then it would be like this
1
|
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
If we want to do same thing in hard way , then it will be look like this:
1
|
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
1
|
sed '1d' reg
|
Delete line 1-3 of lines
1
|
sed '1,3d' reg
|
Delete the line if found the search pattern
1
|
sed '/pcspeech/d' reg
|
Append/Insert/Read with sed command
Append a line at the end
1
|
sed '$a\Something different' reg
|
Usually append command append a line after the data stream if we don’t specify an address. For example
1
|
echo "What the" | sed 'a\hell'
|
We can also append a line after specific line number
1
|
sed '2a\Something different' reg
|
Insert command insert the line before all line. For example
1
|
echo "What the" | sed 'i\hell'
|
1
2
3
4
5
6
7
8
9
|
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
1
2
3
|
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
1
|
sed '2r reg1.txt' reg
|
See the Last line
What if you want to print only last line?
1
|
sed -n '$p' reg
|
Cleaning up html tags from a file
1
|
sed 's/<[^>]*>//g ; /^$/d'
|
No comments:
Post a Comment