Sponsored Content
Top Forums Shell Programming and Scripting replace one section in a datafile Post 74775 by Paprika on Monday 13th of June 2005 12:10:37 PM
Old 06-13-2005
Tools replace a paragraph in a textfile

Hi:
First, this is not a homework problem. I just need enough of a hint to get this going...
My datafile (dataf.in) is made up of 10 sections. Each section begins with & and with &&
So it looks like this:-------------------------------------
&section1
...etc...
&&

&section2
...etc...
&&

------------------

I need to replace section2 with a new section that I call "section_new" contained
in file (dataf.new). So now, dataf.in looks like this now:
&section1
...etc...
&&

&section_new
...etc...
&&


I am trying to do that with no luck with a shell script or awk.
Is that feasible? or should I be coding it in C?
Any suggestion or feedback is appreciated.

Best,
Paprika Smilie

Last edited by Paprika; 06-14-2005 at 10:54 AM.. Reason: want to include additional comment.
 

10 More Discussions You Might Find Interesting

1. Solaris

oracle datafile *dbf

Hi ,,,, I have move an oracle db from old server to a new server ( solaris 5.9 is the operating system ) my problem is that to new server the datafile ( *.dbf ) are in a different path ..... example old : /export/home/data/blobs ........... new /oracle/data/blobs....... how i can... (3 Replies)
Discussion started by: tt155
3 Replies

2. Shell Programming and Scripting

selective positions from a datafile

Hi dear friends, Im writing a shell script which has to select the strings based on the position. but the problem is there is no field seperator. Normally a datafile contains 2000 records (lines) and each line is of size 500 charecters. I want to select the fields from all the lines which... (10 Replies)
Discussion started by: ganapati
10 Replies

3. Shell Programming and Scripting

Combine a datafile with Master datafile, emergent!

Hi guys, my supervisor has asked me to solve the problem in 7 days, I've taken 3 days to think about it but couldn't figure out any idea. Please give me some thoughts with the following problem, I have index.database that has only index date: 1994 1995 1996 1997 1998 1999 I have... (6 Replies)
Discussion started by: onthetopo
6 Replies

4. UNIX for Dummies Questions & Answers

How do I read/find/replace fields in a csv datafile?

hello. I'm somewhat a novice here so please be patient. My stumbling block when loading csvs into ORACLE tables is this: I need to read a csv datafile, check several fields in each line, and if any of stated fields contain A ZERO only then replace it with a null/blank character. I had a... (9 Replies)
Discussion started by: MrCarter
9 Replies

5. UNIX for Advanced & Expert Users

How do we know which processis creating a datafile

Hi, Is there any way we can find out which process is creating a partucular datafile.I know the user and group but i am just curios to know is there any way to find the process. Thanks (7 Replies)
Discussion started by: ukatru
7 Replies

6. Shell Programming and Scripting

Extract section of file based on word in section

I have a list of Servers in no particular order as follows: virtualMachines="IIBSBS IIBVICDMS01 IIBVICMA01"And I am generating some output from a pre-existing script that gives me the following (this is a sample output selection). 9/17/2010 8:00:05 PM: Normal backup using VDRBACKUPS... (2 Replies)
Discussion started by: jelloir
2 Replies

7. Shell Programming and Scripting

sorting the datafile in an order given in second datafile

Hi, I have two files: first input file is having 7-8 columns, and second data file is like I want to arrange my datafile1 in the order given in second data file, by comparing the seconddatafile with the second column of first file and print the entire line....also if any... (2 Replies)
Discussion started by: CAch
2 Replies

8. Shell Programming and Scripting

Prepend first line of section to each line until the next section header

I have searched in a variety of ways in a variety of places but have come up empty. I would like to prepend a portion of a section header to each following line until the next section header. I have been using sed for most things up until now but I'd go for a solution in just about anything--... (7 Replies)
Discussion started by: pagrus
7 Replies

9. Shell Programming and Scripting

Validating a datafile with the datatypes

I have two input files 1)datafile 2)metadata file. I have a metadata file like: field1datatypeformat1number2string3dateyy-mm-dd I have a data file like: 1234abc12-8-16 xyz234512-9-163456acd14-08-12 In the first row there is no correction as everything is inline with the metadata.... (3 Replies)
Discussion started by: bikky6
3 Replies

10. Shell Programming and Scripting

Read in numbers from a datafile

Hi, I want to be able to read numbers from many files which have the same general form as follows: C3H8 4.032258004031807E-002 Phi = 1.000000E+00 Tau = 5.749E+00 sL0 = 3.805542E+01 dL0 = 1.514926E-02 Tb = 2.328291E+03 Tu = 3.450E+02 Alpha = ... (3 Replies)
Discussion started by: lost.identity
3 Replies
API(1)							User Contributed Perl Documentation						    API(1)

NAME
PDL::API - making piddles from Perl and C/XS code DESCRIPTION
A simple cookbook how to create piddles manually. It covers both the Perl and the C/XS level. Additionally, it describes the PDL core routines that can be accessed from other modules. These routines basically define the PDL API. If you need to access piddles from C/XS you probably need to know about these functions. SYNOPSIS
use PDL; sub mkmypiddle { ... } Creating a piddle manually from Perl Sometimes you want to create a piddle manually from binary data. You can do that at the Perl level. Examples in the distribution include some of the IO routines. The code snippet below illustrates the required steps. use Carp; sub mkmypiddle { my $class = shift; my $pdl = $class->new; $pdl->set_datatype($PDL_B); my @dims = (1,3,4); my $size = 1; for (@dims) { $size *= $_ } $pdl->setdims([@dims]); my $dref = $pdl->get_dataref(); # read data directly from file open my $file, '<data.dat' or die "couldn't open data.dat"; my $len = $size*PDL::Core::howbig($pdl->get_datatype); croak "couldn't read enough data" if read( $file, $$dref, $len) != $len; close $file; $pdl->upd_data(); return $pdl; } Creating a piddle in C The following example creates a piddle at the C level. We use the "Inline" module which is really the way to interface Perl and C these days. Note the use of the "PDL_INCLUDE", "PDL_TYPEMAP", "PDL_AUTO_INCLUDE" and "PDL_BOOT" functions that were imported from "PDL::Core::Dev". They are used in conjunction with an Inline Config call to ensure that the PDL typemap, the PDL include files and the PDL Core routines are found during compilation and later runtime execution. use PDL::LiteF; use PDL::Core::Dev; $a = myfloatseq(); # exercise our C piddle constructor print $a->info," "; # the reason for this config call is explained below use Inline C => Config => INC => &PDL_INCLUDE, # make sure we find pdlcore.h etc TYPEMAPS => &PDL_TYPEMAP, # use the PDL typemap AUTO_INCLUDE => &PDL_AUTO_INCLUDE, # global declarations and includes BOOT => &PDL_BOOT; # boot code to load the Core struct use Inline C; Inline->init; # useful if you want to be able to 'do'-load this script __DATA__ __C__ static pdl* new_pdl(int datatype, PDL_Long dims[], int ndims) { pdl *p = PDL->pdlnew(); PDL->setdims (p, dims, ndims); /* set dims */ p->datatype = datatype; /* and data type */ PDL->allocdata (p); /* allocate the data chunk */ return p; } pdl* myfloatseq() { PDL_Long dims[] = {5,5,5}; pdl *p = new_pdl(PDL_F,dims,3); PDL_Float *dataf = (PDL_Float *) p->data; int i; for (i=0;i<5*5*5;i++) dataf[i] = i; /* the data must be initialized ! */ return p; } Wrapping your own data into a piddle Sometimes you obtain a chunk of data from another source, for example an image processing library, etc. All you want to do in that case is wrap your data into a piddle struct at the C level. Examples using this approach can be found in the IO modules (where FastRaw and FlexRaw use it for mmapped access) and the Gimp Perl module (that uses it to wrap Gimp pixel regions into piddles). The following script demonstrates a simple example: use PDL::LiteF; use PDL::Core::Dev; use PDL::Graphics::PGPLOT; $b = mkpiddle(); print $b->info," "; imag1 $b; use Inline C => Config => INC => &PDL_INCLUDE, TYPEMAPS => &PDL_TYPEMAP, AUTO_INCLUDE => &PDL_AUTO_INCLUDE, BOOT => &PDL_BOOT; use Inline C; Inline->init; __DATA__ __C__ /* wrap a user supplied chunk of data into a piddle * You must specify the dimensions (dims,ndims) and * the datatype (constants for the datatypes are declared * in pdl.h; e.g. PDL_B for byte type, etc) * * when the created piddle 'npdl' is destroyed on the * Perl side the function passed as the 'delete_magic' * parameter will be called with the pointer to the pdl structure * and the 'delparam' argument. * This gives you an opportunity to perform any clean up * that is necessary. For example, you might have to * explicitly call a function to free the resources * associated with your data pointer. * At the very least 'delete_magic' should zero the piddle's data pointer: * * void delete_mydata(pdl* pdl, int param) * { * pdl->data = 0; * } * pdl *p = pdl_wrap(mydata, PDL_B, dims, ndims, delete_mydata,0); * * pdl_wrap returns the pointer to the pdl * that was created. */ typedef void (*DelMagic)(pdl *, int param); static void default_magic(pdl *p, int pa) { p->data = 0; } static pdl* pdl_wrap(void *data, int datatype, PDL_Long dims[], int ndims, DelMagic delete_magic, int delparam) { pdl* npdl = PDL->pdlnew(); /* get the empty container */ PDL->setdims(npdl,dims,ndims); /* set dims */ npdl->datatype = datatype; /* and data type */ npdl->data = data; /* point it to your data */ /* make sure the core doesn't meddle with your data */ npdl->state |= PDL_DONTTOUCHDATA | PDL_ALLOCATED; if (delete_magic != NULL) PDL->add_deletedata_magic(npdl, delete_magic, delparam); else PDL->add_deletedata_magic(npdl, default_magic, 0); return npdl; } #define SZ 256 /* a really silly function that makes a ramp image * in reality this could be an opaque function * in some library that you are using */ static PDL_Byte* mkramp(void) { PDL_Byte *data; int i; if ((data = malloc(SZ*SZ*sizeof(PDL_Byte))) == NULL) croak("mkramp: Couldn't allocate memory"); for (i=0;i<SZ*SZ;i++) data[i] = i % SZ; return data; } /* this function takes care of the required clean-up */ static void delete_myramp(pdl* p, int param) { if (p->data) free(p->data); p->data = 0; } pdl* mkpiddle() { PDL_Long dims[] = {SZ,SZ}; pdl *p; p = pdl_wrap((void *) mkramp(), PDL_B, dims, 2, delete_myramp,0); /* the delparam is abitrarily set to 0 */ return p; } The gory details The Core struct -- getting at PDL core routines at runtime PDL uses a technique similar to that employed by the Tk modules to let other modules use its core routines. A pointer to all shared core PDL routines is stored in the $PDL::SHARE variable. XS code should get hold of this pointer at boot time so that the rest of the C/XS code can then use that pointer for access at run time. This initial loading of the pointer is most easily achieved using the functions "PDL_AUTO_INCLUDE" and "PDL_BOOT" that are defined and exported by "PDL::Core::Dev". Typical usage with the Inline module has already been demonstrated: use Inline C => Config => INC => &PDL_INCLUDE, TYPEMAPS => &PDL_TYPEMAP, AUTO_INCLUDE => &PDL_AUTO_INCLUDE, # declarations BOOT => &PDL_BOOT; # code for the XS boot section The code returned by "PDL_AUTO_INCLUDE" makes sure that pdlcore.h is included and declares the static variables to hold the pointer to the "Core" struct. It looks something like this: print PDL_AUTO_INCLUDE; #include <pdlcore.h> static Core* PDL; /* Structure holds core C functions */ static SV* CoreSV; /* Gets pointer to perl var holding core structure */ The code returned by "PDL_BOOT" retrieves the $PDL::SHARE variable and initializes the pointer to the "Core" struct. For those who know their way around the Perl API here is the code: print PDL_BOOT; perl_require_pv ("PDL::Core"); /* make sure PDL::Core is loaded */ CoreSV = perl_get_sv("PDL::SHARE",FALSE); /* SV* value */ #ifndef aTHX_ #define aTHX_ #endif if (CoreSV==NULL) Perl_croak(aTHX_ "We require the PDL::Core module, which was not found"); PDL = INT2PTR(Core*,SvIV( CoreSV )); /* Core* value */ if (PDL->Version != PDL_CORE_VERSION) Perl_croak(aTHX_ "The code needs to be recompiled against the newly installed PDL"); The "Core" struct contains version info to ensure that the structure defined in pdlcore.h really corresponds to the one obtained at runtime. The code above tests for this if (PDL->Version != PDL_CORE_VERSION) .... For more information on the Core struct see PDL::Internals. With these preparations your code can now access the core routines as already shown in some of the examples above, e.g. pdl *p = PDL->pdlnew(); By default the C variable named "PDL" is used to hold the pointer to the "Core" struct. If that is (for whichever reason) a problem you can explicitly specify a name for the variable with the "PDL_AUTO_INCLUDE" and the "PDL_BOOT" routines: use Inline C => Config => INC => &PDL_INCLUDE, TYPEMAPS => &PDL_TYPEMAP, AUTO_INCLUDE => &PDL_AUTO_INCLUDE 'PDL_Corep', BOOT => &PDL_BOOT 'PDL_Corep'; Make sure you use the same identifier with "PDL_AUTO_INCLUDE" and "PDL_BOOT" and use that same identifier in your own code. E.g., continuning from the example above: pdl *p = PDL_Corep->pdlnew(); Some selected core routines explained The full definition of the "Core" struct can be found in the file pdlcore.h. In the following the most frequently used member functions of this struct are briefly explained. o "pdl *SvPDLV(SV *sv)" o "pdl *SetSV_PDL(SV *sv, pdl *it)" o "pdl *pdlnew()" "pdlnew" returns an empty pdl object that needs further initialization to turn it into a proper piddle. Example: pdl *p = PDL->pdlnew(); PDL->setdims(p,dims,ndims); p->datatype = PDL_B; o "pdl *null()" o "SV *copy(pdl* p, char* )" o "void *smalloc(int nbytes)" o "int howbig(int pdl_datatype)" o "void add_deletedata_magic(pdl *p, void (*func)(pdl*, int), int param)" o "void allocdata(pdl *p)" o "void make_physical(pdl *p)" o "void make_physdims(pdl *p)" o "void make_physvaffine(pdl *p)" o "void qsort_X(PDL_Xtype *data, int a, int b)" and "void qsort_ind_X(PDL_Xtype *data, int *ix, int a, int b)" where X is one of B,S,U,L,F,D and Xtype is one of Byte, Short, Ushort, Long, Float or Double. o "float NaN_float" and "double NaN_double" These are constants to produce the required NaN values. SEE ALSO
PDL Inline BUGS
This manpage is still under development. Feedback and corrections are welcome. COPYRIGHT
Copyright (c) 2001, Christian Soeller. All Rights Reserved. This module is free software. It may be used, redistributed and/or modified under the same terms as PDL itself (see http://pdl.perl.org). perl v5.12.1 2009-10-17 API(1)
All times are GMT -4. The time now is 09:04 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy