sed + match line and add word on the first string in line
I have the following file
mo开发者_如何学Gore file
machine1 network netmask broadcast
how to add the "_NAME" after the first word in the line that have the "network netmask broadcast" words ?
remark sed also need to match only "network netmask broadcast" and then to add _NAME to the first word in the line
example: what I need to get after execute sed command
machine1_NAME network netmask broadcast
This should work, since we're not using the g
modifier, it'll only match once per line
sed -e 's/^[^ ]*\>/&_NAME/'
It matches non-space characters up to the first word boundary, replacing it with itself and appends _NAME
. As mentioned, without the g
modifier it'll only match once per line, and if there are leading white space on the line, just remove the first ^
-anchor.
Edit
you only wanted it to match on specific lines, so here goes:
sed -e '/network netmask broadcast$/s/^[^ ]*\>/&_NAME/'
The first part is a selector, which makes sure the substitution is only performed on lines where network netmask broadcast
ends the line. To have it match any lines with those words, just remove the $
-anchor, and add *
(space-asterisk) to the spaces to make them flexible. But you probably already knew that.. :)
sed '/network.*netmask.*broadcast/s|^\([ \t]*\)\(.[^ \t]*\) |\1\2_NAME|' file
精彩评论