Perl Script Not Reading Input Files Correctly


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Script Not Reading Input Files Correctly
# 1  
Old 03-16-2011
Perl Script Not Reading Input Files Correctly

This is one of the strangest things that's happening to me.
I'm writing a new Perl script that is trying to read a file.
The file is originally in .mof format, but I also saved the contents into a .txt file.

As a simple test, I wrote this:

Code:
#!/user/bin/perl -w
use strict;

#open(NODEFILE, "<nodes.mof");
open(NODEFILE, "<test.txt");
my $count = 0;
while (<NODEFILE>) {
    my $line = $_;
    #chomp($line);
    print "$line";
    if ($count == 3) {
        last;
    }
    $count++;
}
close(NODEFILE);

Whether I open the .mof file or .txt file, I always get this output:
■p r a g m a n a m e s p a c e ( " \ \ \ \ . \ \ R o o t \ \ O p e n v i e w \ \ d a t a " )

i n s t a n c e o f O V _ N o d e G r o u p
{

The result should appear like this:
#pragma namespace("\\\\.\\Root\\Openview\\data")

instance of OV_NodeGroup


So why the heck would the script be putting those spaces when reading the file??
# 2  
Old 03-16-2011
Was this input file edited in notepad by any chance? It looks like it's been saved as 16-bit Unicode -- the weird char at the beginning is a special marker telling Windows it's a unicode .txt file, and the characters are all 16-bit, with the high byte blank because you're using glyphs <256.
# 3  
Old 03-17-2011
Ahh, I see.
In that case, any other format for the characters would work properly?
# 4  
Old 03-17-2011
That's what the contents of the file really are, so it is reading it fine. You just got Unicode when you expected ASCII. Or got something stranger when you expected ASCII -- what is MOF? If this was never readable text to begin with, just renaming it won't make it suddenly be readable text.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Open Source

Splitting files using awk and reading filename value from input data

I have a process that requires me to read data from huge log files and find the most recent entry on a per-user basis. The number of users may fluctuate wildly month to month, so I can't code for it with names or a set number of variables to capture the data, and the files are large so I don't... (7 Replies)
Discussion started by: rbatte1
7 Replies

2. Shell Programming and Scripting

Input not saving correctly in both files

If the user inputs two variants separated by a comma, the below command is supposed to write both variants to the GJ-53.txt and then to out.txt. Both are written to the GJ-53.txt, however only one is written to out.txt and I'm not sure why. Thank you :). gjb2() { printf "\n\n" ... (1 Reply)
Discussion started by: cmccabe
1 Replies

3. Shell Programming and Scripting

Reading CLI input for script

I've always written scripts where the user executes the script and I prompt them for what they want to do. But I'm trying to write a script where root executes the script 'lock' or its hard-link 'unlock' and the script will passwd -l or passwd -u an account depending on the choice. What would... (3 Replies)
Discussion started by: ADay2Long
3 Replies

4. Shell Programming and Scripting

Help with reading two input files in awk

Hello, I'm trying to write an awk program that reads two files inputs. example, file 1: 0.00017835 0.000176738 0.00018811 0.000189504 0.000188155 0.000180065 0.000178991 0.000178252 0.000182513 file 2: 1.7871769E-05 1.5139576E-16 1.5140196E-16 1.5139874E-16 1.7827407E-04 ... (5 Replies)
Discussion started by: joseamck
5 Replies

5. Shell Programming and Scripting

perl how to exit a while loop and quit reading the input file

I am reading a file using While loop while <FILE> { $_ = <FILE>; process data... } I would like to quit reading the file once I encounter a String pattern. How do i do it. is it if (/SUMMARY/) { last; } I am having problems with uninitialized value in pattern... (1 Reply)
Discussion started by: subhap
1 Replies

6. Shell Programming and Scripting

write a perl script or kornshell reading a two files and outputting to comma format

Hello Can someone help me to write a perl script or kornshell reading a two files and outputting to comma format. Here is the two files listofdisks.txt id, diskname, diskgroup, diskisze(GB), FC 1, CN34, GRP1, 30, FC_CN34 2, CN67, GRP5, 19, 4, VD1, GRP4, 23, FC_VD1 6, CF_D1, ... (0 Replies)
Discussion started by: deiow
0 Replies

7. Shell Programming and Scripting

Reading input files

Okay, so I've looked on here and found some similar things, but not exactly what I am looking for. I am working on creating a script that can back up some files, based on the contents of another file - the configuration file. First file contains the files to back up - we'll call this... (1 Reply)
Discussion started by: pdxwarrior
1 Replies

8. Shell Programming and Scripting

Reading specific contents from 1 input files and appending it to another input file

Hi guys, I am new to AWK and unix scripting. Please see below my problem and let me know if anyone you can help. I have 2 input files (example given below) Input file 2 is a standard file (it will not change) and we have to get the name (second column after comma) from it and append it... (5 Replies)
Discussion started by: sksahu
5 Replies

9. Shell Programming and Scripting

awk reading 2 input files but not getting expected value

I'm reading 2 input files but not getting expected value. I should get an alpha value on file_1_data but not getting any. Please help. >cat test6.sh awk ' FILENAME==ARGV { file_1_data=$0; print "----- 1 Line " NR " -----" $1; next } FILENAME==ARGV { file_2_data=$0; print "----- 2... (1 Reply)
Discussion started by: pdtak
1 Replies

10. UNIX for Dummies Questions & Answers

Reading Input in a Script

#!/usr/bin/sh echo "Enter reason:" echo "> \c" read $reason $reason >> access.log This doesnt work for me. Can someone tell me how I would read the input from what the person types, and then append that to the log file? Regards (2 Replies)
Discussion started by: alwayslearningunix
2 Replies
Login or Register to Ask a Question