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 :)<br><br>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!
<br><br>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.
<br><br>sed 's/something/complex/g' file<br><br>is the same as<br><br>perl -pe 's/something/complex/g' file<br><br>and you can do a lot more things in the code. You can even edit a file in place with -i:<br>
<br>perl -i.bak -pe 's/sed/perl -pe/' myscript.sh<br><br>or only print certain lines with -n (no print rather than the -p meaning print)<br><br>perl -ne 'if (/\/([^\/]+$)/) { print $1; }' file<br><br>(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)
<br><br>Sean<br><br><div><span class="gmail_quote">On 5/9/07, <b class="gmail_sendername">Dan Martin</b> <<a href="mailto:ummar143@cc.umanitoba.ca">ummar143@cc.umanitoba.ca</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I tried to use a sed command to extract alphanumberic names, one per<br>line, from a file which also included comments (line starts with '#')<br>and blank lines. I wanted to refer to each in a loop.<br><br>I tried
<br>for MACHINE in `sed '/^[0-9A-Za-z][0-9A-Za-z]*/'<br>~/MPI-SRC/machine_names.txt`<br>a little awkward because sed has no '+' to indicate "at least one<br>alphanumeric".<br><br>I got an error that there was no command given to sed. I thought it
<br>printed by default.<br><br>I tried<br>for MACHINE in `sed '/^[0-9A-Za-z][0-9A-Za-z]*/p'<br>~/MPI-SRC/machine_names.txt`<br>and it printed all the names twice.<br><br>Finally, I had to use<br><br>for MACHINE in `sed -n '/^[0-9A-Za-z][0-9A-Za-z]*/p' ~/MPI-SRC/machine_names.txt`
<br><br>to suppress one copy.<br><br>Why do I get "double or nothing"?<br><br><br>--<br> -Dan<br><br>Dr. Dan Martin, MD, CCFP, BSc, BCSc (Hon)<br><br>GP Hospital Practitioner<br>Computer Science grad student<br>
<a href="mailto:ummar143@cc.umanitoba.ca">ummar143@cc.umanitoba.ca</a><br>(204) 831-1746<br>answering machine always on<br><br>_______________________________________________<br>Roundtable mailing list<br><a href="mailto:Roundtable@muug.mb.ca">
Roundtable@muug.mb.ca</a><br><a href="http://www.muug.mb.ca/mailman/listinfo/roundtable">http://www.muug.mb.ca/mailman/listinfo/roundtable</a><br><br></blockquote></div><br><br clear="all"><br>-- <br>Sean Walberg <<a href="mailto:sean@ertw.com">
sean@ertw.com</a>> <a href="http://ertw.com/">http://ertw.com/</a>