Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Write all STDIN lines into a file

Status
Not open for further replies.

azoulay

Technical User
May 12, 2011
5
CA
I want to write all(5) user lines from STDIN into a file.

This is my code, it doesn't work:

================================
#!/usr/perl5/bin/perl

open (TMPMSG,">tempFile.txt");

while (<STDIN>) {
print TMPMSG $_;
}
close (TMPMSG);
================================

I tried many version of this code but it never works.
I'm missing something.
Could someone please help
 
Code:
#!/usr/perl5/bin/perl

use strict;
use warnings;

my $file = 'tempFile.txt';
open my $fh, '>', $file or die $!;

print "Enter blank line to end\n";

while (<STDIN>) {
	last if /^$/;
	print $fh $_;
}
close $fh;
 
Writing the prompt to STDOUT doesn't make much sense to me. In General, STDIN/STDOUT won't be connected to a terminal.
 
How can you make such a generalisation? It depends entirely on how the script is normally invoked.

Annihilannic.
 
Thanks a lot MillerH,

your code works perfectly, it does exactly what I was expecting.

For rovf comment, I work for an ISP and we need to take some show output from Cisco equipments and copy and paste SOME of the output to a file. It's just quicker/easier to do this this way.

Thanks again MillerH,

regards,

Jonathan
 
> How can you make such a generalisation? It depends entirely on how the script is normally invoked.

Exactly that's the point. The OP does not reveal how s/he normally invokes the script, hence we can not assume that STDOUT connects to a terminal. That's what "In general..." means.

BTW, it's great that your solution works for the poster, even though it doesn't fulfil the original requirements, because in general (again!), it copies neither ALL lines from STDIN, nor does it copy exactly 5 lines from STDIN (the OP's specification is a bit inconsistent in this respect), as was required. Instead, it copies from STDIN until it encounters an empty line.
 
True. Wasn't my solution though. :)

Strangely, the OPs code worked fine for me unmodified.

Annihilannic.
 
> Strangely, the OPs code worked fine for me unmodified.

Well, if the OP really used his code from the command line,
maybe he simply forgot to signal EOF to STDIN....
 
That was my guess too, an omission that MillerH's code handles neatly.

Annihilannic.
 
> "...forgot to signal EOF to STDIN"

what is that exactly? How do you signal EOF?

thanks again

 
> Ctrl-D on a blank line.

I don't understand the difference of my code and MillerH code in regards to the signal EOF to STDIN. Where does MillerH include the signal EOF to STDIN in his code ?

Regards
 
It doesn't, the Ctrl-D is supplied by the user entering the data.

As I mentiond, your original code works fine for me:

Code:
$ cat azoulay
#!/usr/bin/perl -w

use strict;

open (TMPMSG,">tempFile.txt");

while (<STDIN>) {
        print TMPMSG $_;
}
close (TMPMSG);
$ ./azoulay
this is
a test
lakdsfjsdk
                               [green]<--- I typed Ctrl-D followed by Enter here[/green]
$ cat tempFile.txt
this is
a test
lakdsfjsdk
$

What happened when you ran your code? When you did, did you hit Ctrl-D to end the text being entered?

Basically MillerH's code means that Ctrl-D is not required because it stops reading the input when it detects and empty/blank line.

Annihilannic.
 
Ohhh ok I understand now !!!

I was not doing a CTRL-D, I was doing a CTRL-Z and the consequence was that the file it is suppose to write in stayed empty.

You're right, my code does work now that I do a CTRL-D.

Thank you so much for this nuance I did not know.

Regards,

 
>> How do you signal EOF?
> Ctrl-D on a blank line.

For completeness, we should mention that this is true on Unix-like systems. For Windoze CMD shell, for instance, it would be Ctrl-Z. Though the #! line of the OP's program suggests that he is indeed using such a system, we can not conclusively conclude it.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top