How to create files with two or more variables in its names?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to create files with two or more variables in its names?
# 1  
Old 08-27-2013
IBM How to create files with two or more variables in its names?

Hi all,
Iam writing a perl script to create many files with variables in their name.
i am able to do it, if iam using only one variable. But with two variables the file
names are NOT getting generated in the way i want.

plz help me out.

Code:
1. open(SHW,">divw_unsigned_50_50_$k.reset") or die $!;

output files generated:
divw_unsigned_50_50_0.reset    divw_unsigned_50_50_1.reset  divw_unsigned_50_50_2.reset   divw_unsigned_50_50_3.reset   

2. open(SHW,">divw_unsigned_$p_50_$k.reset") or die $!;

output files generated:
divw_unsigned_0.reset    divw_unsigned_1.reset 
divw_unsigned_2.reset   divw_unsigned_3.reset

Here in 2nd case the numbers are the values of $k. But $p_50 is getting ignored. $p is some other variable whose value also varies.
# 2  
Old 08-27-2013
You are actually using the variable "$p_50_" and this is most likely unset, so it contains an empty string.

I hope this helps.

bakunin
# 3  
Old 08-27-2013
Try
${p_50} for variable p_50
${p} for variable p

Code:
open(SHW,">divw_unsigned_${p_50}_${k}.reset") or die $!;

This User Gave Thanks to Jotne For This Post:
# 4  
Old 08-27-2013
IBM

HI Thanks for reply,

$p is the value iam input iam reading

Code:
my $p = <STDIN>;
print "p: $p:;
open(SHW,">divw_unsigned_$p_50_$k.reset") or die $!;
print SHW "STICK divw_unsigned.cfg_32  ",  ($p==32)&1,  "\n";

I have added a print of $p and it is printing the value given in <STDIN>;
and iam using $p for some operation in the files created and that operation is working fine. Problem is with ONLY file names.

---------- Post updated at 04:27 PM ---------- Previous update was at 04:19 PM ----------

Thanks all for your quick reply.

Code:
open(SHW,">divw_unsigned_${p}_${i}_${k}.reset") or die $!;
or
open(SHW,">divw_unsigned_${p}_${i}_$k.reset") or die $!;
output files generated:
divw_unsigned_44_33_0.reset 
divw_unsigned_44_33_1.reset
divw_unsigned_44_33_2.reset
divw_unsigned_44_33_3.reset



both of these are working. But I want to know why? please educate me on this.
# 5  
Old 08-27-2013
If you are mixing text and variable, always use curly bracket.
This way you are 100% sure what is variable or not.
$k. its ignoring the . and threats the variable as $k
$p_50 Here bash handles _ and 50 as part of variable name. So variable gets name $p_50 and not $p

Code:
k=2
echo "$k.tt"
2.tt

echo "$k_tt"
<nothing>

echo "${k}_tt"
2_tt

This User Gave Thanks to Jotne For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Create one xml file from one list of names

Actually I have one list of channels names like: Rai 1 Rai 1 +1HD Rai 1 +2HD Rai 2 Rai 2 +1HD Rai 2 +2HD . . . .From this list of names I need create one new xml file with this structure <channel id="Rai 1"> <display-name lang="it">Rai 1</display-name> <icon... (3 Replies)
Discussion started by: Tapiocapioca
3 Replies

2. Shell Programming and Scripting

awk to create variables to pass into a bash loop to create a download link

I have created one file that contains all the necessary info in it to create a download link. In each of the lines /results/analysis/output/Home/Auto_user_S5-00580-6-Medexome_67_032/plugin_out/FileExporter_out.67... (8 Replies)
Discussion started by: cmccabe
8 Replies

3. Shell Programming and Scripting

Exclude certain file names while selectingData files coming in different names in a file name called

Data files coming in different names in a file name called process.txt. 1. shipments_yyyymmdd.gz 2 Order_yyyymmdd.gz 3. Invoice_yyyymmdd.gz 4. globalorder_yyyymmdd.gz The process needs to discard all the below files and only process two of the 4 file names available ... (1 Reply)
Discussion started by: dsravanam
1 Replies

4. Shell Programming and Scripting

Korn Shell help - Using parameter to create variable names

I'm using korn shell and I am wondering if it's possible to use a parameter passed into a function to build a variable name in a configuration file. I have the function in one source file, I'd like to have a global configuration file instead of hardcoding logins to each script. So I have a... (7 Replies)
Discussion started by: mrevello
7 Replies

5. Shell Programming and Scripting

Searching for file names with variables

Hello everyone We have a problem about searching and copying files with variables. we have variables like $year $jday $date and we want to search the files whose name contain these variables. we tried *$year*$jday*$date or with ? instead of * thank you everyone!!! (4 Replies)
Discussion started by: miriammiriam
4 Replies

6. Shell Programming and Scripting

Read variables names from array and assign the values

Hi, I have requirement to assign values to variables which are created dynamically. Below is the code which i am using to achieve above requirement. #!/bin/ksh oIFS="$IFS"; IFS=',' STR_FAIL_PARENT_IF_FAILS="WF_F_P_IF_FAILS1,WF_F_P_IF_FAILS2,WF_F_P_IF_FAILS3" set -A... (1 Reply)
Discussion started by: tmalik79
1 Replies

7. Shell Programming and Scripting

AWK: Retrieving names of variables passed with -v

I'm an experienced awk user, but this one has me stumped. I have an awk script which is called from a UNIX command line as you'd expect: myscript.awk -v foo=$1 -v bar=$2 filename My question is this: is there a mechanism for determining the names of the -v variables within a script? ... (3 Replies)
Discussion started by: John Mac
3 Replies

8. Shell Programming and Scripting

Parsing Directory Names for Use as Bash Variables

Hi all: I have a directory where all of the subdirectories are named by the convention "images_#1:#2_Date." My goal is to get an array for each subdirectory that has the structure (#1,#2, int). I am able to use awk to print each subdirectory's values, but cannot figure out how to get them into an... (6 Replies)
Discussion started by: aefskysa
6 Replies

9. UNIX Desktop Questions & Answers

trying to create a script with multiple variables...

I have created a script that prompts the user to enter three variables that are seperated by a space as the delimiter. It then performs a command 3 seperate times for each variable entered. I want the script to llow the user to enter as many variables as they may like and the script to... (5 Replies)
Discussion started by: Italy87
5 Replies

10. UNIX for Advanced & Expert Users

Inserting variables from 2 files to create 1 file

Hi, I have 2 files which are as follows: Num: 548983 748932 783928 547383 839284 Grp: 2389 9873 8493 7382 8493 I need to make an output file using both of these files. (1 Reply)
Discussion started by: rochitsharma
1 Replies
Login or Register to Ask a Question