Perl Help - Assigning variables to text file contents


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Help - Assigning variables to text file contents
# 1  
Old 05-03-2011
Perl Help - Assigning variables to text file contents

I am looking to create a perl script which will take numbers from a simple text file, convert them from decimal to hex, and then rewrite those values in the file or create a new file with the hex numbers(whichever's easier).

My text document for example would be something as simple as

1312
4646
4231
3566
1341
2331

I know how to use sprintf, but I don't know how to make the function convert an entire file. I can only get it to convert one number that I input by myself. Does anyone know how to do this? Any help would be appreciated
# 2  
Old 05-03-2011
You will have to read the file line by line and convert it.
For a start, this is how you read file line by line in perl

Code:
#open a file for writing the new vlues
open OUTFILE, ">your_new_file_name" or die "Error : Could not open [$!]";

my $readLine = "";
open FILE, "your_file_name" or die "Error : Could not open [$!]";
while (<FILE>) 
{   
  chomp;
  $readLine=$_;
  print $readLine, "\n";
  # Do you conversion here and write into another file
   print OUTFILE "Converted Value";
}

regards,
Ahamed
# 3  
Old 05-03-2011
Hi, I am sorry I am very new at perl. Can you please show me how to write the converted number to another file? Also, if I keep writing to the second file one number at a time, will it overwrite anything? I want to make a new file that has several converted numbers


Thank you

---------- Post updated at 02:32 PM ---------- Previous update was at 02:15 PM ----------

This is what I have so far, but all I'm getting back is the name of the file dec.txt. I've created a file named dec.txt and hex.txt for this script.


Code:
 
#!/usr/bin/perl
#open a file for writing the new vlues
open OUTFILE, ">hex.txt" or die "Error : Could not open [$!]";
my $readLine = "";
open FILE, "dec.txt" or die "Error : Could not open [$!]";
while (<dec.txt>)
{
  chomp;
  $readLine=$_;
  print $readLine, "\n";
  # Do you conversion here and write into another file
  $hexval = sprintf("%x", $readline);
  open FILE, ">hex.txt" or die $!;
  print FILE $hexval;
  close FILE;
  print OUTFILE "Converted Value";
}

# 4  
Old 05-03-2011
The first thing I notice is that your variable isn't being called correctly.

Code:
 
$hexval = sprintf("%x", $readline);

Change to
Code:
$hexval = sprintf("%x", $readLine);

I hate perl, but it looks like your conversion is okay except for that. Change that variable name and see if anything comes out. Let me know what output you're getting to hex.txt after the change if you're still having issues.
# 5  
Old 05-04-2011
I actually noticed that same error and fixed it but got the same result. I am getting just a 0 as an output. Everytime I rerun the file, it adds another 0.
# 6  
Old 05-05-2011
Try this

Code:
#!/usr/bin/perl

my $readLine = "";
#open a file for writing the converted values
open OUTFILE, ">hex.txt" or die "Error : Could not open [$!]";
#open the file to read the dec values
open INFILE, "dec.txt" or die "Error : Could not open [$!]";
while (<INFILE>)
{
  chomp;
  $readLine=$_;
  # Do you conversion here and write into another file
  $hexval = sprintf("%x", $readLine);
  print OUTFILE $hexval,"\n";
}
close INFILE;
close OUTFILE;

check your code, the variable name is $readLine not $readline. Alos made some changes to the file operation.
cheers!

regards,
Ahamed
# 7  
Old 05-06-2011
One liner.
Code:
perl -nle 'BEGIN{open(OUT,">","hex.txt") or die "$!";} print OUT sprintf("%x",$_);END{close OUT;}' dec.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching a file - and assigning responses to variables (or something)

So first: Sorry if the title is confusing... I have a script I'm writing with a file with several names in it (some other info - but it's not really pertinent...) - I want to be allow the user to delete certain records, but I ran into a problem I'm not sure how to go about fixing. If I were... (6 Replies)
Discussion started by: sabster
6 Replies

2. Shell Programming and Scripting

Reading from a file and assigning to an array in perl

I wrote a simply perl that searched a file for a particualr value and if it found it, rite it and the next three lines to a file. Now I have been asked to check those next three lines for a different value and only write those lines if it finds the second value. I was thinking the best way to... (1 Reply)
Discussion started by: billprice13
1 Replies

3. Shell Programming and Scripting

Bash: Reading a file and assigning variables from file

I have a file that has four values on each line and I'd like to give each column a variable name and then use those values in each step of a loop. In bash, I believe you could use a while loop to do this or possibly a cat command, but I am super new to programming and I'm having trouble decoding... (2 Replies)
Discussion started by: ccorder22
2 Replies

4. Shell Programming and Scripting

Getting value from one file and assigning it to variables of another file.

Hi, I need to read two values,startdate and enddate from a param file and then write this value to another param file(global) under a specific workflow name.Here in global param file there is may workflow names, hence we need to check for the right one and then add the data below it. Any... (5 Replies)
Discussion started by: angel12345
5 Replies

5. Shell Programming and Scripting

Help in assigning values to variables from the file

Hi! This might be a simple thing, but I'm struggling to assign values to variables from the file. I've the following values stored in the file.. It consists of only two rows.. 10 20 I want to assign the first row value to variable "n1" and the second row value to variable "n2".. That is ... (3 Replies)
Discussion started by: abk07
3 Replies

6. UNIX for Dummies Questions & Answers

File Field Replacement, Assigning Fields to Variables, Lists/Arrays?

Okay, I've made threads on extracting fields and comparing strings in separate files in .csv's. I've written the following code with intentions of learning more. I just want this one question answered: How can I assign fields from a file(comma separated) to variables? My goal is to check... (0 Replies)
Discussion started by: chickeneaterguy
0 Replies

7. Shell Programming and Scripting

Assigning a value as a variable from a text file

I have a txt file output.txt Freq = 1900 L = 159I want to assign the values to a variable so that i can further use it in some other script. like F=1900 Len=159 etc i tried doing something with awk but dosent work F=$(awk 'BEGIN {}/Freq/ {split ($2,a);depth=a};printf "%d\t,... (2 Replies)
Discussion started by: shashi792
2 Replies

8. UNIX for Dummies Questions & Answers

Assigning value in a text file to a variable

Hi, I need to place a number located in a text file in a variable so I can perform if/then comparison. How would I go about doing this? Using A=awk '{print $2}' maintenance_date.tmp does not seem to work. Thanks (1 Reply)
Discussion started by: mojoman
1 Replies

9. Shell Programming and Scripting

Assigning file attributes to variables

Hi, I'm trying to assign the permissions, owner and group of a file to seperate variables, but using ls -l filename | awk '{print $1 "\t" $3 "\t" $4}' gives the owner as tom.ja instead of tom.james Is there any way to expand it so i get the full name, or is there an easier way to get them... (5 Replies)
Discussion started by: olimiles
5 Replies

10. Shell Programming and Scripting

PERL:print 32 variables into a text file

okay, down below is the script. i have 32 words put into 32 variables and i want perl to print all those 32 variables into one text document, each word under another in the text file. the text files called times.txt Sorry about the length of the script print " VToshiba ... (2 Replies)
Discussion started by: perleo
2 Replies
Login or Register to Ask a Question