Trouble reading content of file from a variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trouble reading content of file from a variable
# 1  
Old 12-20-2013
Trouble reading content of file from a variable

Hi ,

i have a parameter which has path of a file. Now i need to have another parameter with the content of that file. I tried the belwo script , can any one please help.
I dont want to use cat command to read. Can we do it with out using cat command.
Code:
 while read line
do
DML_FILE_NAME_ORIG1=`echo $line|awk 'BEGIN { FS = "|" } ; {print $1}'`
 
echo "DML_FILE_NAME_ORIG1=$DML_FILE_NAME_ORIG1"
 
DML_FILE_NAME_NEW_MODIFIED=${AI_DML}/testharness_modified.dml
cp $DML_FILE_NAME_ORIG1 ${AI_DML}/testharness_modified.dml
sed -i '/record/r ${AI_BIN}/modift_test_harness.txt' ${AI_DML}/testharness_modified.dml
 
echo "Contents of DML_FILE_NAME_NEW_MODIFIED"
echo `cat $DML_FILE_NAME_NEW_MODIFIED`
done<$AI_BIN/input.txt

Code:
> cat AI_BIN/input.txt
$AI_DML/a.dml
 
> cat $AI_DML/a.dml
include '~$STI_TOOLS_DML/data_quality.dml';
metadata type =
/* DML Generated for SQL: SELECT * FROM cdm_5.FT_ARNGE_CRED_RISK_MTGT_FACT
 * On: Thu Mar 29 06:40:06 2012
 */
record
  decimal("\x01") mth_rpt_perd_dim_id /*INTEGER NOT NULL*/;
  decimal("\x01") core_arnge_dim_id /*INTEGER NOT NULL*/;
end
 
> cat ${AI_BIN}/modift_test_harness.txt
string("\x01") key1 = NULL("");
string("\x01") key2 = NULL("");


Last edited by Ravindra Swan; 12-20-2013 at 12:45 PM.. Reason: content missed
# 2  
Old 12-20-2013
The real question is, why do you want to dump the entire content of a file into a variable? Can you just use the file as-is?
# 3  
Old 12-20-2013
I don't see where AI_DML gets set, but I presume it is an environmental variable.

You can remove the complex echo | awk to be just:-
Code:
DML_FILE_NAME_ORIG1="${line%%|*}"

.... or perhaps change the loop:-
Code:
OIFS="$IFS"
IFS="|"
while read DML_FILE_NAME_ORIG1 rest
do
   echo "DML_FILE_NAME_ORIG1=$DML_FILE_NAME_ORIG1"
   DML_FILE_NAME_NEW_MODIFIED=${AI_DML}/testharness_modified.dml
   cp $DML_FILE_NAME_ORIG1 ${AI_DML}/testharness_modified.dml
   sed -i '/record/r ${AI_BIN}/modift_test_harness.txt' ${AI_DML}/testharness_modified.dml
 
   echo "Contents of DML_FILE_NAME_NEW_MODIFIED"
   cat $DML_FILE_NAME_NEW_MODIFIED
  
done < $AI_BIN/input.txt
IFS="$OIFS"

You will notice I have removed the echo `cat and just left the cat

What are you actually trying to achieve here?



Robin
This User Gave Thanks to rbatte1 For This Post:
# 4  
Old 12-20-2013
Its not working

Thanks for the reply....

I need to dump into variable only because in further steps i need to pass variable to ETL-graph and then it will execute using this.

Environmental issues are not there since i tried to run the script using hard coded value and it ran perfectly. Using variable only the problem comes.

cp command is not working ...... is there any other way around?

---------- Post updated at 12:13 AM ---------- Previous update was at 12:09 AM ----------

Hi Robin,

I just need the content of the file where the file path is in parameter to another parameter where it will be also having another file path with different name.

cp is not working is there any way around?

Phani

Quote:
Originally Posted by rbatte1
I don't see where AI_DML gets set, but I presume it is an environmental variable.

You can remove the complex echo | awk to be just:-
Code:
DML_FILE_NAME_ORIG1="${line%%|*}"

.... or perhaps change the loop:-
Code:
OIFS="$IFS"
IFS="|"
while read DML_FILE_NAME_ORIG1 rest
do
   echo "DML_FILE_NAME_ORIG1=$DML_FILE_NAME_ORIG1"
   DML_FILE_NAME_NEW_MODIFIED=${AI_DML}/testharness_modified.dml
   cp $DML_FILE_NAME_ORIG1 ${AI_DML}/testharness_modified.dml
   sed -i '/record/r ${AI_BIN}/modift_test_harness.txt' ${AI_DML}/testharness_modified.dml
 
   echo "Contents of DML_FILE_NAME_NEW_MODIFIED"
   cat $DML_FILE_NAME_NEW_MODIFIED
  
done < $AI_BIN/input.txt
IFS="$OIFS"

You will notice I have removed the echo `cat and just left the cat

What are you actually trying to achieve here?



Robin
# 5  
Old 12-20-2013
Not sure I understand your specification
Quote:
I just need the content of the file where the file path is in parameter to another parameter where it will be also having another file path with different name.
Please describe in more detail and provide examples.
Quote:
cp command is not working ...... is there any other way around?
cp has proven to be working a zillion times. What is its behaviour in your case that displeases you? Run it with the -x option set and show us the execution log.
# 6  
Old 12-20-2013
I mean

a.ksh
a=/ai/a.txt
b=/ai/b.txt
cp $a $b
echo `cat $b`
-----------------
cat /ai/a.txt
and
cat /ai/b.txt both should give the same result set.
# 7  
Old 12-20-2013
This is wrong:

Code:
echo `cat $b`

The `cat $b` is going to flatten out all whitespace.

It's not the cp that does it, it's the ``.

It's completely redundant, anyway. Just cat $b would accomplish the same thing without the mangling.

It's this kind of thing which makes me suspect you don't need cat or cp at all, here, if we understood your requirements better.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Reading flat file content

is there any unix tools that can read the text files like through SQL queries? (ie in Hadoop, Impala DB support flat file query) (1 Reply)
Discussion started by: omykarshan
1 Replies

2. Shell Programming and Scripting

Trouble reading from a tab delimited excel file

So I have a file1.txt that is tab delimited: e.g. FIELD1 FIELD2 FIELD3 FIELD4 FIELD5 9545641 123 "Neighbor and Labrador,Canada" 54895 'CANADA' 9456465 456 "Neighbor and Labrador,Canada" 54893 'CANADA' 8746512 789 "Neighbor and... (11 Replies)
Discussion started by: dan139
11 Replies

3. UNIX for Dummies Questions & Answers

Getting ls content into a file using variable

hi i just cant figure out how can i do this ls -lt > log.txt using $PWD what i mean is how can i get the ls command content into a file using $PWD variable? :confused: (4 Replies)
Discussion started by: chinababy
4 Replies

4. Shell Programming and Scripting

Trouble with variable assignment and reading in shell scripting

Hi, I have an inputfile with some table names in it. For ex: t_arnge t_profl t_fac I need a script which reads the line one by one and need to assign to some dynamic variable. If to explain the above example: i need some a=table_name1 table_name1=t_arnge in first time and... (4 Replies)
Discussion started by: Ravindra Swan
4 Replies

5. Shell Programming and Scripting

Reading files under a folder and formatting content of each file

I have 'n' number of files in a folder .each file in the folder "myfolder" is having the content like. COLNAME ------------ AAAAAA BBBBBB CCCCCC DDDDDD ... ... ... ZZZZZZ 26 recrod(s) selected. My request is by reading each file in "myfolder" and format each file such a way... (18 Replies)
Discussion started by: rocking77
18 Replies

6. Shell Programming and Scripting

Trouble reading [noeol] file

cat can't show the last line of a file sftp transffered from Windows, because the last line is not ended with new line. Vi can show all line with warning I can append blank line in order for cat to show all lines, But is there a command to do this without modifying the file? > cat -A... (3 Replies)
Discussion started by: honglus
3 Replies

7. Shell Programming and Scripting

Reading content of a variable to create a new one?

Hello. I've written up a script, that populates a variable with a list of tapes returned from my library. For example: 701940L3,701941L3,701942L3,701943L3,701944L3,701945L3,701946L3,701947L3,701948L3 So now, the variable "TAPELIST" contains those numbers, delimited by commas. I'd like to... (6 Replies)
Discussion started by: Stephan
6 Replies

8. Shell Programming and Scripting

Problem in reading a file content

Hi, I am reading a file line by line using read line function of while loop. Each line contains 4 fields. I want to take these 4 values in 4 variables in each iteration so that i can use them in my script. The issue here is that my awk command is returning awkward results - Here is a sample line... (8 Replies)
Discussion started by: garman
8 Replies

9. Shell Programming and Scripting

Reading variable from file variable values

Hi, Here is the output of lpstat. I would like to read value of Queue which is(abxxxxb1)and status that is DOWN in first line. i dont care what is in second line. any one can help me.thanks Queue Dev Status Job Files User PP % Blks Cp Rnk ------- ----- ---------... (5 Replies)
Discussion started by: sagii
5 Replies

10. UNIX for Advanced & Expert Users

Reading a file and sending mail based on content of the file

Hi Gurus, I am having an requirement. i have to read a list file which contains file names and send mail to different users based on the files in the list file. eg. if file a.txt exists then send a mail to a@a.com simillary for b.txt,c.txt etc. Thanks for your help, Nimu (6 Replies)
Discussion started by: nimu1979
6 Replies
Login or Register to Ask a Question