Read Single Value From File With Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read Single Value From File With Perl
# 1  
Old 04-01-2009
Read Single Value From File With Perl

Hi all,

I have what I would have thought was a very simple problem but I can' find an elegant solution.

I have a file which has a single value you in it, say 194. All I want my perl script to do is open the file, read the value and assign that value to a variable.

I've done stuff like this with large files where I've written everything to an array like so:
Code:
$file="newfile";
open(DAT, $file) || die("Could not open file!");
@raw_data=<DAT>;
close(DAT);

foreach $line (@raw_data)
{
chop($line);
($cc,$c_name,$H1,$H2)=split(/,/,$line);
}


Something like the above seems excessive for just reading a file with a single value in it.

Is there a one liner or similar for this?

Thanks!!!

Last edited by Yogesh Sawant; 04-01-2009 at 02:29 PM.. Reason: added code tags
# 2  
Old 04-01-2009
Code:
$ cat myprog
#!/usr/bin/perl

use strict;
use warnings;
use English qw( -no_match_vars );

my $dataFH;
my $line;
my $cc;
my $c_name;
my $H1;
my $H2;
my $file = 'newfile';

open $dataFH, "<", $file or die "Cannot open file $file:  $OS_ERROR\n";

while ( $line = <$dataFH> ) {
  chomp ($line);
  ($cc, $c_name, $H1, $H2) = split(/,/, $line);

  print "cc=$cc\n";
  print "c_name=$c_name\n";
  print "H1=$H1\n";
  print "H2=$H2\n";
}

close $dataFH or die "Cannot close $file:  $OS_ERROR\n";

exit 0;

$ cat newfile
cc, c_name, H1, H2

$ ./myprog
cc=cc
c_name= c_name
H1= H1
H2= H2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Creating script to read many files and load into database via single control file

Hi, I have many files but with only 2 names , I want to load the data of that file into database through sqlldr with single control file. how can i do that ????? Example: switch_file switch_file billing_file billing_file now these files should be loaded into same database but different... (1 Reply)
Discussion started by: niti_sharma
1 Replies

2. Shell Programming and Scripting

Tabbed multiple csv files into one single excel file with using shell script not perl

Hi Experts, I am querying backup status results for multiple databases and getting each and every database result in one csv file. so i need to combine all csv files in one excel file with separate tabs. I am not familiar with perl script so i am using shell script. Could anyone please... (4 Replies)
Discussion started by: ramakrk2
4 Replies

3. Shell Programming and Scripting

Perl script to read string from file#1 and find/replace in file#2

Hello Forum. I have a file called abc.sed with the following commands; s/1/one/g s/2/two/g ... I also have a second file called abc.dat and would like to substitute all occurrences of "1 with one", "2 with two", etc and create a new file called abc_new.dat sed -f abc.sed abc.dat >... (10 Replies)
Discussion started by: pchang
10 Replies

4. Shell Programming and Scripting

editing single line in html file in perl script

Hi Folks, It is regarding the perl scripting. I have an html file(many files) which contains the below line in the body tag. <body> <P><STRONG><FONT face="comic sans ms,cursive,sans-serif"><EM>Hello</EM></FONT></STRONG></P> </body> Now I want to read that html file through perl... (3 Replies)
Discussion started by: giridhar276
3 Replies

5. Shell Programming and Scripting

Replace single quote with two single quotes in perl

Hi I want to replace single quote with two single quotes in a perl string. If the string is <It's Simpson's book> It should become <It''s Simpson''s book> (3 Replies)
Discussion started by: DushyantG
3 Replies

6. Shell Programming and Scripting

Perl:Read single value from text file and assign to variable

Hello All, A part of my very basic perl code requires me to read a single value from a text file. The file output is the following: Reading image ... done IMAGEREGION=0x0x0-256x162x256 VOXELDIMENSION=0.9375000000x1.2000000477x0.9375000000 VOXELNUMBER=10527001... (7 Replies)
Discussion started by: ncl
7 Replies

7. Shell Programming and Scripting

perl storing single value from file

Hi, I have a configuration file that contains one word that I want to store as a variable to use later in my perl program. i.e. #cat /opt/dti/ssh_user root I want to store root as a variable in perl. In bash I would do this as user=$(cat /opt/dti/ssh_user) (1 Reply)
Discussion started by: borderblaster
1 Replies

8. Shell Programming and Scripting

Read Oracle Username password SID from single file and pass it to shell

Dear All I am trying to write one shell which will be running through Cron which contain one SQL query. But I want to draw/fetch the Username password and Instance name (required to loging to the database) from one single file to run that SQL query . Also this file contain details of multiple... (2 Replies)
Discussion started by: jhon
2 Replies

9. Shell Programming and Scripting

awk, perl Script for processing a single line text file

I need a script to process a huge single line text file: The sample of the text is: "forward_inline_item": "Inline", "options_region_Australia": "Australia", "server_event_err_msg": "There was an error attempting to save", "Token": "Yes", "family": "Family","pwd_login_tab": "Enter Your... (1 Reply)
Discussion started by: hmsadiq
1 Replies

10. Shell Programming and Scripting

Need shell script to read two file at same time and print out in single file

Need shell script to read two file at same time and print output in single file Example I have two files 1) file1.txt 2) file2.txt File1.txt contains Aaa Bbb Ccc Ddd Eee Fff File2.txt contains Zzz Yyy Xxx (10 Replies)
Discussion started by: sreedhargouda
10 Replies
Login or Register to Ask a Question