sed lines in script - exercise
a simple question please
i have this code, and it add the word echo for all lines, but i want exclusively to odd lines
i kwow that this code sed -n 1~2p' sh开发者_运维百科ow me all odd lines, but i can't doing the same in script above
sed 's/.*/echo &/' $startdirectory
thanks
For a literal answer to what you're asking (apply an 's' action to every other line), you want
sed -e '1~2s/.*/echo &/'
This is marginally better than the N
way of doing things in that it doesn't interfere with other things you might want to do to other lines in the file.
Actually rather simple:
sed -e 'N;s/^/echo /'
The N reads a second line into the pattern space; the substitute puts 'echo' in front of the first, the implicit print prints both lines and empties the pattern space.
Note that if you have an odd number of lines, it drops the last. Fixing that is an exercise for the reader.
精彩评论