How to do a formatted substittion in perl with a csh variable?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to do a formatted substittion in perl with a csh variable?
# 1  
Old 01-10-2009
How to do a formatted substittion in perl with a csh variable?

Hmm quite difficult for me to express my problem in one sentence.Introduction [which is recomended to be skipped] {i am a newbee to csh perl, awk and stuff, and by surfing these forums i managed to build quite a big code using all three of them in a script. Have to admitt, it was not a good idea, and its the perfect example of how not to program, but i have to little time to learn a complete script language like perl. And i needed some stuff, which i wasnt sure if perl could do it, or at least i didnt find how } #end of time wasitng intro in a csh script i have the following two lines:set t1g = `perl -e "printf '%07g', $t1-$dt1"`perl -pi -e "s/%%var1%/$t1g/" main_opt.kfirst one is meant to calculate a value of a csh variable which consists of floating points numbers read from a specific line and column text file elswhere in the code. i call perl since it can do the floating point opperations.second line is meant to substitute the value of %%var1% in a text file with the number calculated in the first line. like:R T2 %%var1% R T3 %%var2% R T4 3.260000R R T6 3.660000R T7 3.860000R T8 4.060000R Now the problem, i would like perl to preplace it with a floating point number of formatted length 7 in my case. now the number has different lengths some times its 0.125 and some times its 0.3333333 or 923.24 how can i get it to a fixed length? (0.12500 0.33333 923.24) such that the other values in the text file dont move.Hope some one can help. Thank you verry much.Radamez
# 2  
Old 01-10-2009
Hmm quite difficult for me to express my problem in one sentence.
Introduction [which is recomended to be skipped]
{i am a newbee to csh perl, awk and stuff, and by surfing these forums i managed to build quite a big code using all three of them in a script.
Have to admitt, it was not a good idea, and its the perfect example of how not to program, but i have to little time to learn a complete script language like perl. And i needed some stuff, which i wasnt sure if perl could do it, or at least i didnt find how
} #end of time wasitng intro

in a csh script i have the following two lines:


set t1g = `perl -e "printf '%07g', $t1-$dt1"`
perl -pi -e "s/%%var1%/$t1g/" main_opt.k


first one is meant to calculate a value of a csh variable which consists of floating points numbers read from a specific line and column text file elswhere in the code. i call perl since it can do the floating point opperations.

second line is meant to substitute the value of %%var1% in a text file with the number calculated in the first line.
like:

R T2 %%var1% R T3 %%var2% R T4 3.260000R
R T6 3.660000R T7 3.860000R T8 4.060000R

Now the problem, i would like perl to preplace it with a floating point number of formatted length 7 in my case. now the number has different lengths some times its 0.125 and some times its 0.3333333 or 923.24 how can i get it to a fixed length? (0.12500 0.33333 923.24) such that the other values in the text file dont move.

Hope some one can help. Thank you verry much.

Radamez
# 3  
Old 01-10-2009

Why didn't you edit your original post instead of making a new one?
Quote:
Originally Posted by Radamez
Hmm quite difficult for me to express my problem in one sentence.
Introduction [which is recomended to be skipped]

Then why post it?
Quote:
{i am a newbee to csh perl, awk and stuff, and by surfing these forums i managed to build quite a big code using all three of them in a script.

csh is not recommended for scripting.
Quote:
Have to admitt, it was not a good idea, and its the perfect example of how not to program, but i have to little time to learn a complete script language like perl. And i needed some stuff, which i wasnt sure if perl could do it, or at least i didnt find how

You can do almost everything in a POSIX shell, with a twist of awk.
Quote:
} #end of time wasitng intro

in a csh script i have the following two lines:

Please put code inside [code] tags.
Quote:
Code:
set t1g = `perl -e "printf '%07g', $t1-$dt1"`
perl -pi -e "s/%%var1%/$t1g/" main_opt.k

first one is meant to calculate a value of a csh variable which consists of floating points numbers read from a specific line and column text file elswhere in the code. i call perl since it can do the floating point opperations.

awk is simpler than perl.
Quote:
second line is meant to substitute the value of %%var1% in a text file with the number calculated in the first line.
like:

R T2 %%var1% R T3 %%var2% R T4 3.260000R
R T6 3.660000R T7 3.860000R T8 4.060000R

Now the problem, i would like perl to preplace it with a floating point number of formatted length 7 in my case. now the number has different lengths some times its 0.125 and some times its 0.3333333 or 923.24 how can i get it to a fixed length? (0.12500 0.33333 923.24) such that the other values in the text file dont move.

In any language which has printf (awk, POSIX shell, etc.) use a format string: "%.5f".
# 4  
Old 01-11-2009
Dear Mr. cfajohnson

Your post was not verry helpfull to me. Nevertheless I Thank you for your precious time.

Maybe some one else could Answer my question in a way that i understand.

Many Thanks in Advance
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Formatted output in PERL

Hi Techies, I'm a newbie to PERL, Please help me with following problem. I have an input text file like below cat Input.txt 418673132,P 492538858,P 384535478,P 521522357,I 529435679,I 183617024,P 184414408,I 735510689,P 736238343,I 411642045,I 412690979,I 104232783,I (2 Replies)
Discussion started by: mahi_mayu069
2 Replies

2. Shell Programming and Scripting

Csh variable calling problem

First post on here. So I use csh shells for my research (physics... not a CS person). I am trying to rerun the same scripts, but there are ~10 files that have similar variables that I have to change for each different configuration, so I would like one central file for the variables I change that... (3 Replies)
Discussion started by: sabrepride
3 Replies

3. Shell Programming and Scripting

Perl Csh - setenv ENV change environment variable

I have 3 programs, 1 in perl, 2 in csh: call them perl1, csh1 and run.ol I need perl1 to set csh1 variable NOLOG_qsub = "" I need perl1 to run, run.ol run.ol takes the executable and input and outputs to output run.ol#!/bin/csh -f # run.ol executable input output perl1 should... (1 Reply)
Discussion started by: austinj
1 Replies

4. Shell Programming and Scripting

Reading a variable in csh

I have a simple script that sets a value and reads the value in csh: set -x set a = 10 echo $a The output of the script does not show the value of a + set a = 10 + echo any help would be great. (4 Replies)
Discussion started by: pt14
4 Replies

5. Shell Programming and Scripting

help with multiline variable in csh

My shell is csh and it is required. I have a file like sample.txt ------------------------ a b c d e f g h i ------------------------ I want set the file to a variable and print it out in the same format. I have tried something like this, but not succed. % cat ~/tmp/sample.txt a b c d... (8 Replies)
Discussion started by: anykao
8 Replies

6. Shell Programming and Scripting

awk, sed, perl assistance in outputting formatted file

Hello, Please advise. Scoured this site, as well as google for answers. However if you do not know what to search for, it's a bit hard to find answers. INPUT: ACTASS= 802 BASECOS= 279 COSNCHG= 3 CUSCOS= 52 UPLDCOS= 2 DESIRED OUTPUT: ACTASS=802 BASECOS=279 (13 Replies)
Discussion started by: abacus
13 Replies

7. UNIX for Dummies Questions & Answers

move a variable value from csh to awk ...

Guys, following is my issue: ------------------------ #!/bin/csh # specify a counter @ k = 1 # loop while ($k < 3) # read all text file with a specific number less than 3 in the last column of a record foreach file ( *.txt) # capture the line by passing the column... (1 Reply)
Discussion started by: znbhatti
1 Replies

8. UNIX for Dummies Questions & Answers

address variable content with csh

By using a csh, I want to address a variable content whose name is/matches the content of a given other variable. i.e. set name=´sam´ set ${name}_age=´27´ So, by typing: echo ${name}_age I correctly obtain: sam_age By typing: echo $sam_age or echo ${sam_age} I correctly obtain: 27 ... (1 Reply)
Discussion started by: sobolev
1 Replies

9. Shell Programming and Scripting

csh formatted strings

Hi, currently I'm trying to write a csh script that should do the following: set i = 1 while ($i < 100) { cp AAA BBB set i = $i +1 } where AAA is a string like this file.11.txt, file.21.txt, ... and BBB is a string like this file_0001, file_0002, ... Is it possible to... (1 Reply)
Discussion started by: sera1711
1 Replies

10. Shell Programming and Scripting

Pass csh variable to Perl

Hi All, I am trying to put the csh variable into a perl. In the below case, i am trying to put the csh variable "var" into my perl code. I tried to use '"$var"' but i don;t think it works. Can anybody help me pls? #!/bin/csh set var = `echo "xxx"` perl myperlcode.pl file ... (9 Replies)
Discussion started by: Raynon
9 Replies
Login or Register to Ask a Question