Home » Infrastructure » Unix » writing to a file.bit urgent.
writing to a file.bit urgent. [message #98166] Mon, 19 April 2004 06:18 Go to next message
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 Go to previous messageGo to next message
Frank Naude
Messages: 4579
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 Go to previous messageGo to next message
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 Go to previous messageGo to next message
Venky
Messages: 52
Registered: October 2001
Member
Hi Frank,

The fields in data file are separated by pipe (&#124). So when I tried to change the code it started giving error ....not found.
My existing data file is like below

PNC04212004.dat
----------------
xyz&#124abc?&#124949;
abc&#124xzy&#124123;?
mno&#124asdf‚ðƒm

PNC04222004.dat
---------------
asdf&#124gfg??
lkjlj&#124lhhi??
jghj&#124hhhh??

Now with unix shell scripting I need a single file PNC.dat like below

PNC.dat
-------
PNC04212004&#124xyz&#124abc?&#124949;
PNC04212004&#124abc&#124xzy&#124123;?
PNC04212004&#124mno&#124asdf‚ðƒm
PNC04222004&#124asdf&#124gfg??
PNC04222004&#124lkjlj&#124lhhi??
PNC04222004&#124jghj&#124hhhh??

Please do the needful.

Regards,

Venky.
Re: writing to a file.bit urgent. [message #98171 is a reply to message #98168] Wed, 21 April 2004 03:01 Go to previous messageGo to next message
Frank Naude
Messages: 4579
Registered: April 1998
Senior Member
Hi,

Create a new variable from ${filename}, let's call it ${file_without_ext}:

file_without_ext=`echo ${filename} | cut -d'.' -f1`


Best regards.

Frank
Re: writing to a file.bit urgent. [message #98173 is a reply to message #98171] Wed, 21 April 2004 03:11 Go to previous messageGo to next message
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 Go to previous message
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(&#124). There are two files as given below.

PNC04212004.dat
----------------
xyz&#124abc?
abc&#124xzy&#12433;
mno&#124asdf‚134

PNC04222004.dat
---------------
asdf&#124gfg?
lkjlj&#124lhhi&#124456;
jghj&#124hhhh?

Now with unix shell scripting I need a single file PNC.dat like below

PNC.dat
-------
PNC04212004&#124xyz&#124abc?
PNC04212004&#124abc&#124xzy&#12433;
PNC04212004&#124mno&#124asdf‚134
PNC04222004&#124asdf&#124gfg?
PNC04222004&#124lkjlj&#124lhhi&#124456;
PNC04222004&#124jghj&#124hhhh?

Please help me....

Regards,

Venky.
Previous Topic: problem with installation of patch IY28766 onRS6000
Next Topic: Oracle on DGUX
Goto Forum:
  


Current Time: Fri Mar 29 07:26:03 CDT 2024