I guess the simple answer is because you told it to...  the p command prints the current pattern space, the -n suppress it.  I'm not sure how to use sed like grep, which is basically what you're doing :)

This may be a silly observation, but a regexp like ^[0-9A-Za-z][0-9A-Za-z]* is the same as ^[0-9A-Za-z] (with or without a + at the end) if you're looking for a simple match and not replacing anything. The second alphanum doesn't have to match at all because of the *, so there's not much point in having it!

Quite frankly sed is too much of a pain for anything but the most simple substitutions.  In your case I'd look at egrep (which supports the + operator, previous comment notwithstanding) , and for anything more complex, a perl one liner. 

sed 's/something/complex/g' file

is the same as

perl -pe 's/something/complex/g' file

and you can do a lot more things in the code.  You can even edit a file in place with -i:

perl -i.bak -pe 's/sed/perl -pe/' myscript.sh

or only print certain lines with -n (no print rather than the -p meaning print)

perl -ne 'if (/\/([^\/]+$)/) { print $1; }' file

(the last one should print the last component of a /path/to/file/name of lines looking like a file path, and nothing on the rest)

Sean

On 5/9/07, Dan Martin <ummar143@cc.umanitoba.ca> wrote:
I tried to use a sed command to extract alphanumberic names, one per
line, from a file which also included comments (line starts with '#')
and blank lines.  I wanted to refer to each in a loop.

I tried
for MACHINE in `sed  '/^[0-9A-Za-z][0-9A-Za-z]*/'
~/MPI-SRC/machine_names.txt`
a little awkward because sed has no '+' to indicate "at least one
alphanumeric".

I got an error that there was no command given to sed.  I thought it
printed by default.

I tried
for MACHINE in `sed  '/^[0-9A-Za-z][0-9A-Za-z]*/p'
~/MPI-SRC/machine_names.txt`
and it printed all the names twice.

Finally, I had to use

for MACHINE in `sed -n '/^[0-9A-Za-z][0-9A-Za-z]*/p' ~/MPI-SRC/machine_names.txt`

to suppress one copy.

Why do I get "double or nothing"?


--
  -Dan

Dr. Dan Martin, MD, CCFP, BSc, BCSc (Hon)

GP Hospital Practitioner
Computer Science grad student
ummar143@cc.umanitoba.ca
(204) 831-1746
answering machine always on

_______________________________________________
Roundtable mailing list
Roundtable@muug.mb.ca
http://www.muug.mb.ca/mailman/listinfo/roundtable




--
Sean Walberg < sean@ertw.com>    http://ertw.com/