Perl: How to read text from file and process $variable in that data too.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl: How to read text from file and process $variable in that data too.
# 1  
Old 03-23-2012
[Solved] Perl: How to read text from file and process $variable in that data too.

In the hello.htm have the sentenses:
Code:
Hello $name 
How are you?

The perl script:
Code:
$name = "David";

open(HEADER,"hello.htm");
while(<HEADER>) { $html .= $_; }
close(HEADER);

print "$html";

I making something about template. But it can't process the $name variable.

Last edited by natong; 03-23-2012 at 03:12 PM..
# 2  
Old 03-23-2012
perl

Quote:
Originally Posted by natong
In the hello.htm have the sentenses:
Code:
Hello $name 
How are you?

The perl script:
Code:
$name = "David";
 
open(HEADER,"hello.htm");
while(<HEADER>) {$_ =~ s/(.*)(Hello)(.*)/$1$2 $name $3/gi;$html .= $_; }
close(HEADER);
 
print "$html";


I making something about template. But it can't process the $name variable.
Here i am substitute the name after "hello" found.
i modifier in pattern matching denotes ignore case.
s denotes sunstitution
g denotes global.

Cheers,
RangaSmilie

Last edited by rangarasan; 03-23-2012 at 08:19 AM..
This User Gave Thanks to rangarasan For This Post:
# 3  
Old 03-23-2012
Good idea. Use search and replace. No other solution?

Code:
$name = "David";

open(HEADER,'hello.htm');
while(<HEADER>) { $_ =~ s/\$name/$name/g; $html .= $_; }
close(HEADER);

print "$html";

# 4  
Old 03-23-2012
You can do in various ways, But search and replace is the better way i think,
# 5  
Old 03-23-2012
Code:
 
$_ =~ s/\$(\w+)/${$1}/g;    #I don't know why didn't work
$_ =~ s/(\$\w+)/$1/eeg;     #Work!

These can replace any variables. Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

In PErl script: need to read the data one file and generate multiple files based on the data

We have the data looks like below in a log file. I want to generat files based on the string between two hash(#) symbol like below Source: #ext1#test1.tale2 drop #ext1#test11.tale21 drop #ext1#test123.tale21 drop #ext2#test1.tale21 drop #ext2#test12.tale21 drop #ext3#test11.tale21 drop... (5 Replies)
Discussion started by: Sanjeev G
5 Replies

2. Shell Programming and Scripting

Read csv file, convert the data and make one text file in UNIX shell scripting

I have input data looks like this which is a part of a csv file 7,1265,76548,"0102:04" 8,1266,76545,"0112:04" I need to make the output data should look like this and the output data will be part of text file: 7|1265000 |7654899 |A| 8|12660000 |76545999 |B| The logic behind the... (6 Replies)
Discussion started by: RJG
6 Replies

3. UNIX for Beginners Questions & Answers

Shell - Read a text file with two words and extract data

hi I made this simple script to extract data and pretty much is a list and would like to extract data of two words separated by commas and I would like to make a new text file that would list these extracted data into a list and each in a new line. Example that worked for me with text file... (5 Replies)
Discussion started by: dandaryll
5 Replies

4. Shell Programming and Scripting

How to read all data after a specific string from a text file ?

Hi, I have a file(input.txt) and trying to format as output.txt. See the attached file format. Note: This is a windows file (DOS format) and the commands are also going to execute on windows. Basically I am trying to capture all the data in between Local Group Memberships and Global Group... (10 Replies)
Discussion started by: Monoj2014
10 Replies

5. Shell Programming and Scripting

How to read file and load data into a script as variable

I need to read a text file that contain columns of data, i need to read 1st column as a function to call, and others are the data i need to get into a ksh script. I am quite new to ksh scripting, i am not very sure how to read each row line by line and the data in each columns of that line, set... (3 Replies)
Discussion started by: gavin_L
3 Replies

6. Shell Programming and Scripting

Read Data from Config file using Perl

Hi All, Can anyone please explain me how to read data from config file in Perl. Suppose i have a config file named cfile. The data in config file is name=parth lname=mittal user=2007 hostname=fluoride username=parthmittal password=XXXXXX account=unix url=www.unix.com ... (2 Replies)
Discussion started by: parthmittal2007
2 Replies

7. Shell Programming and Scripting

How to read the data from the text file in shell script?

I am having one text file and i need to read that data from my shell script. I will expain you the scenario: Script look like: For name type 1: For age type 2: For Salary type3: echo "Enter the input:" read the data if input is 1 then go to the Text file and print the... (2 Replies)
Discussion started by: dineshmurs
2 Replies

8. 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

9. Shell Programming and Scripting

read in variable data from another file - grep

Hello! I think this should be an easy solution. I have a large file with many fields of data. The first field has a unique identifier (a subject number) for every record for a chunk of data. Something like this: There were ten experimental conditions (ec), but the ec is identified by only... (11 Replies)
Discussion started by: ccox85
11 Replies

10. Shell Programming and Scripting

Read data from a file into a variable

I am a FORTRAN guy and not a UNIX expert by any means so sorry if this sounds dumb, but all I want to do is have a UNIX script which reads data from a file (say 1000 lines worth, each row is a file name) and store it in an array to perform an operation on later. As maddeningly simple as this... (2 Replies)
Discussion started by: yorkdg
2 Replies
Login or Register to Ask a Question