writing to a file.bit urgent. [message #98166] |
Mon, 19 April 2004 06:18 |
Venky
Messages: 52 Registered: October 2001
|
Member |
|
|
Hi,
I have set of data files with the name PNC<date><time>.dat.
The contents of the data file are as follows
B001,RID001,R001,1000,INV001,200,USD
B001,RID001,R001,1000,INV002,300,USD
B001,RID001,R001,1000,INV003,500,USD
I have a process.sql file in unix server. Using HOST command, I have to merge all those files starting with PNC to a single file PNC.dat. This you can achieve with the following command I guess...
HOST cat /<path>/PNC*.dat > /<path>/PNC.dat
But while merging I have to take the corresonding file name and put it as first column in the destination file(PNC.dat) as given below to preserve the reference of the file.
PNC02242004131250,B001,RID001,R001,1000,INV001,200,USD
PNC02242004131250,B001,RID001,R001,1000,INV002,300,USD
PNC02242004131250,B001,RID001,R001,1000,INV003,500,USD
How to do it using unix command. Please help me.
Regards,
Venky.
|
|
|
Re: writing to a file.bit urgent. [message #98167 is a reply to message #98166] |
Tue, 20 April 2004 06:45 |
Frank Naude
Messages: 4581 Registered: April 1998
|
Senior Member |
|
|
Hi,
A bit of shell scripting should save the day:
ls -1 PNC* | while read filename
do
echo Working with file: ${filename}
cat ${filename} | while read line
do
echo ${filename},${line} >>PNC.dat
done
done
PS: I haven't tested the above code, so expect a syntax error or two...
Best regards.
Frank
|
|
|
Re: writing to a file.bit urgent. [message #98168 is a reply to message #98167] |
Tue, 20 April 2004 20:52 |
Venky
Messages: 52 Registered: October 2001
|
Member |
|
|
Hi Frank,
The above code is working fine. But it also takes the extension of the file (ie) PNCXXX.dat. I need only PNCXXX as the first field in the data file. So just tell me what should I do ? I am very weak in unix shell scripting. Please help me.
Regards,
Venky.
|
|
|
Re: writing to a file.bit urgent.one more. [message #98169 is a reply to message #98167] |
Tue, 20 April 2004 21:01 |
Venky
Messages: 52 Registered: October 2001
|
Member |
|
|
Hi Frank,
The fields in data file are separated by pipe (|). So when I tried to change the code it started giving error ....not found.
My existing data file is like below
PNC04212004.dat
----------------
xyz|abc?𞠕
abc|xzy𞓛?
mno|asdf‚ðƒm
PNC04222004.dat
---------------
asdf|gfg??
lkjlj|lhhi??
jghj|hhhh??
Now with unix shell scripting I need a single file PNC.dat like below
PNC.dat
-------
PNC04212004|xyz|abc?𞠕
PNC04212004|abc|xzy𞓛?
PNC04212004|mno|asdf‚ðƒm
PNC04222004|asdf|gfg??
PNC04222004|lkjlj|lhhi??
PNC04222004|jghj|hhhh??
Please do the needful.
Regards,
Venky.
|
|
|
|
Re: writing to a file.bit urgent. [message #98173 is a reply to message #98171] |
Wed, 21 April 2004 03:11 |
Venky
Messages: 52 Registered: October 2001
|
Member |
|
|
Hi Frank,
Sorry for troubling you again. Can you merge the fragment of code and give me the complete code. I just messed up and couldnot get the final result.
Please help me.
I am once again sorry for troubling you.
Regards,
Venky
|
|
|
Re: writing to a file.very urgent. [message #98178 is a reply to message #98169] |
Wed, 21 April 2004 21:15 |
Venky
Messages: 52 Registered: October 2001
|
Member |
|
|
Hi Frank,
Please provide me the complete code for the cited example. Please note that the fields are separated by pipe(|). There are two files as given below.
PNC04212004.dat
----------------
xyz|abc?
abc|xzyゑ
mno|asdf‚134
PNC04222004.dat
---------------
asdf|gfg?
lkjlj|lhhi𞘨
jghj|hhhh?
Now with unix shell scripting I need a single file PNC.dat like below
PNC.dat
-------
PNC04212004|xyz|abc?
PNC04212004|abc|xzyゑ
PNC04212004|mno|asdf‚134
PNC04222004|asdf|gfg?
PNC04222004|lkjlj|lhhi𞘨
PNC04222004|jghj|hhhh?
Please help me....
Regards,
Venky.
|
|
|