Substitution command stream editor for filtering and transforming text
sed 's/as/sd/' <file-name>
sd replace as in file-name output only in first occurrence of line
cat file-name | sed ‘s/as/sd/’
same as above use cat and pipe command in between
Here in this command, we used
s --> used as substitute command / --> used as Delimiter as --> search pattern string sd --> replacement string
Sometime when we try sed on Unix path, we need to use backward slash(Esc character). Like
sed 's//etc/resolv.conf//etc//dns-settings/’
but this create confusion for some users. we can avoid this confusion and replace delimiter with our own choice
sed 's_/etc/resolv.conf_/etc/dns-settings_'
We can also use these settings with global occurrence instead of only first in one line
sed 's/as/sd/g' <file-name>
We can also use these settings for only second occurrence instead of only first in one line
sed 's/as/sd/2' <file-name> sed 's/as/sd/2g' <file-name>
Second and after second occurrence
sed '/root/d' sudoers
Delete the lines that contain root
sed '3d' sudoers
Delete 3rd line
sed -n '/root/p' sudoers
Print only lines containing root
sed ‘/^$/d’ suders
Delete blank lines from file
0 comments:
Post a Comment