milink
1
Hi,
There are total 100 files in the backup directory with the name format as below:
prod_bkp_140611_13_30_05.txt
prod_bkp_140611_14_30_05.txt
prod_bkp_140611_15_30_05.txt
prod_bkp_140611_16_30_05.txt
How to use array variables (in perl) to get list of names of the above files ?
getmmg
2
Is this the output of ls command?
milink
3
yes this is the o/p of ls command.
I want to assign this o/p to the array variable for the further processing in code.
how to assign this values to array variable ?
getmmg
4
This should store the output of ls into the array.
@arr = `ls`;
Use the code below if you want to harcode.
@arr = qw(prod_bkp_140611_13_30_05.txt prod_bkp_140611_14_30_05.txt prod_bkp_140611_15_30_05.txt prod_bkp_140611_16_30_05.txt);
milink
5
Hi,
[oracle@andews:/oracle/backup] $ ls
prod_bkp_140611_13_30_05.txt
prod_bkp_140611_14_30_05.txt
prod_bkp_140611_15_30_05.txt
prod_bkp_140611_16_30_05.txt
[oracle@andews:/oracle/backup] $ @arr=`ls`
-bash: @arr=prod_bkp_140611_13_30_05.txt: command not found
[oracle@andews:/oracle/backup] $ @arr = `ls`
-bash: @arr: command not found
[oracle@andews:/oracle/backup] $ @arr = `ls`;
-bash: @arr: command not found
getmmg
6
Execute with perl.
Some thing like below.
perl -e ' @arr = `ls`;print @arr'
milink
7
Hi,
It works.
Now, I want the stored array values to be used as below in the further code :-
For example:
BackupFile:'prod_bkp_140611_13_30_05.txt','prod_bkp_140611_14_30_05.txt','prod_bkp_140611_15_30_05.txt','prod_bkp_140611_16_30_05.txt'
how it can be done ?
getmmg
8
This should do what you want.
ls | perl -00 -lane 'BEGIN{$,=","}END{print map {qq|\x27$_\x27|} @F;}'
milink
9
Hi,
can u please explain the below line :
print map {qq|\x27$_\x27|} @F;
getmmg
10
27 is the hexcode for ' (single quote) charecter. Since you wanted your output to be wrapped inside it, added that piece of code to map it.
milink
11
Hi,
xtremly sorry .. I am not getting the solution mention by you
but want to insert the file names as shown below in bold:
CREATE TABLE PROD_BKP
(
Column1 NUMBER(20),
Column2 VARCHAR2(25),
Column3 NUMBER(1),
Column4 NUMBER(10),
Column5 NUMBER(10)
)
ORGANIZATION EXTERNAL
( TYPE ORACLE_LOADER
DEFAULT DIRECTORY EXTRACT_DIR
ACCESS PARAMETERS
( RECORDS DELIMITED BY NEWLINE
FIELDS TERMINATED BY '|'
MISSING FIELD VALUES ARE NULL
)
LOCATION (EXTRACT_DIR:'prod_bkp_140611_13_30_05.txt','prod_bkp_140611_14_30_05.txt','prod_bkp_140611_15_30_05.t xt','prod_bkp_140611_16_30_05.txt')
)
REJECT LIMIT UNLIMITED
PARALLEL ( DEGREE 5 INSTANCES 1 )
NOMONITORING;
my @Array = <*.txt>;
print "EXTRACT_DIR:'".join("','", @Array)."'";
This can be solve your problem.
getmmg
13
ls | perl -00 -lane 'BEGIN{$,=","}END{print map {qq|\x27$_\x27|} @F;}'
Consider this as the output of your ls command
File1
File2
This will get stored in @F array like below.
$F[0] = File1
$F[1] = File2
Since you wanted the output separted by comma $, (output separtor in perl) is set as -.
But if you try to print the @F now you'll get an output like this without the single quotes
File1,File2
Since you wanted every item of array @F to be wrapped in single quotes below statement is added before print.
print map {qq|\x27$_\x27|} @F
This is same as writing
print map {"\x27$_\x27"} @F
Map function wraps the array contents inside a single quote. (x27 is hex code equivalent of ' ).
milink
14
Hi,
Instead of 'print' I want the values of @F to be used/added as below in the command:
LOCATION (EXTRACT_DIR:'prod_bkp_140611_13_30_05.txt','prod_bkp_140611_14_30_05.txt','prod_bkp_140611_15_30_05.t xt','prod_bkp_140611_16_30_05.txt')
I tried as below but not working:
LOCATION (EXTRACT_DIR:map {"\x27$_\x27"} @files)