Thread Tools Search this Thread
# 1  


I have a program set to read in a text file, change certain characters and then print the altered version to the screen but does anyone know how to save the new version as another text file? And, if possible, how to specify the file name, and perhaps location?

# 2  
Code: inputfilename > /path/to/outfilefilename

# 3  


Try this one,

#! /usr/bin/perl

my $file = "path/inputfile";
my $outfile = "path/outfile";

if( open(IFILE, "<", "$file") )
    if ( open(OFILE, ">", "$outfile") )
        while(my $line = <IFILE> )

            ## do your changes
            print OFILE "$line\n"; ## print (or write) to the outfile since we use OFILE( this refers outfile)#
        print "Error: Unable to open file $outfile\n";
    print "Error: Unable to open file $file\n";

Here IFILE, OFILE are file handlers. we can make changes of the file with the help of file handler.
Here i used three arguments open function, You can use like below. Dont confuse with this. This is just an alternate way. open file accepts three arguments.
if ( open(IFILE, "<$file") )

we can use select function to select the output stream(STDOUT or FILEHANDLE).
print "Welcome\n"; ##It will write in outfile
print "Hai\n";  ##It will write in outfile 
## Please check that i have not use File Handle but still it will write to the outfile bcos we have selected the default output stream.

After all the print statement will write to outfile since we have selected the default output stream as OFILE.
If you want to print the msg to STDOUT. You have to select,
print "Welcome\n"; # It will print in STDOUT

Ranga Smilie

Last edited by rangarasan; 04-06-2012 at 09:28 AM..
This User Gave Thanks to rangarasan For This Post:

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Science: Mathematics
Difficulty: Easy
A scalene triangle has two sides of equal length.
True or False?

10 More Discussions You Might Find Interesting

1. Cybersecurity

How do I wipe stdout?

Today I used Terminal (invoked from Mac OS X 10.4.11) to view some data that is secured by a password. After exiting Terminal, it occurred to me that a vestige of the data displayed on my screen might reside in memory or in disk file ... the actual file used for stdout, for example. Do I need... (1 Reply)
Discussion started by: dcollins
1 Replies

2. UNIX for Dummies Questions & Answers


Hallo, i have a script like: if ;then echo "OK" else echo "ERROR $2 is missing" fi; if ;then touch $2 fi; if ;then cat $1 | grep xy > $2 (1 Reply)
Discussion started by: eightball
1 Replies

3. Shell Programming and Scripting


Can somebody explain to me why the diff output is not going to stderr? Yet when I issue a diff from the command line the return code is -ne 1. I am guessing diff always writes to stdout??? Is there away I can force the difff to write to stderr USING THE CURRENT template. If possible, I... (5 Replies)
Discussion started by: BeefStu
5 Replies

4. UNIX for Dummies Questions & Answers

csplit to stdout

I want to split big files based on a pattern to stdout. Although csplit works well for me splitting the output into separate files (e.g. xx00, xx01, xx02, ...), the following is not working as expected: <code> # assuming pattern occurs less than 100 times csplit bigfile '%pattern%'... (2 Replies)
Discussion started by: uiop44
2 Replies

5. UNIX for Advanced & Expert Users

How to use gzip on stdout

Does anyone know how I can use gzip to zip a large log file on the fly. My simulation is currently logging a large file that I need for analysis at a later point. However the files are so huge that I may even run out of disk space. The content is mainly text so when compressed the files are... (2 Replies)
Discussion started by: mitch1710
2 Replies

6. UNIX for Dummies Questions & Answers

not able to capture STDOUT

I am using a third party API to get some real time feed. When I run the command it shows the results properly: etd@mhs-apps5009 $ mamalistenc -m lbm -tport mamaqa -S MLALERTS -s KANA wFinancialStatus Type CTRL-C to exit. (null).MLALERTS.KANA Type: INITIAL Status OK wFinancialStatus |... (5 Replies)
Discussion started by: aks__
5 Replies

7. Shell Programming and Scripting

Executing Stdout ???

Hiya all, Simple question - yet no simple answer im afraid ! Is there a way to execute a shell script (child) which returns one line and get the current (parent) shell to execute the stdout from the child ??? example #!/bin/sh echo "setenv DISPLAY xxx:03" parent_prompt>... (5 Replies)
Discussion started by: fawqati
5 Replies

8. Solaris

stdout redirected?

Hi all, I have a c++ program which is running fine but has some printfs which its spews on to the screen, which I need for debugging. But, I execute this program through a java, i.e I run a java program which actually launches my c++ executable and when it does this, I see none of my printfs... (1 Reply)
Discussion started by: Naanu
1 Replies

9. Programming

stdout to a variable?

Hi, I need to set stdout to go to a String variable, has anyone got any ideas? (6 Replies)
Discussion started by: cb.mark
6 Replies

10. UNIX for Dummies Questions & Answers

logging stdout

Hello I run a program that creates number of processes. Is there a way of viewing their standard output on terminal and logging it to a log file simultaneously? (3 Replies)
Discussion started by: masha
3 Replies

Featured Tech Videos