replace one section in a datafile


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace one section in a datafile
# 1  
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.
# 2  
Old 06-16-2005
HTML Code:
(
    awk '
        BEGIN {count=0}
        /^&/ {
            if (substr ($0,1,2) != "&&") ++count
            if (count == 2) exit
        }
        {print}
        ' file1

    cat file2

    awk '
        BEGIN {count=0}
        (count == 2) {print; next}
        /^&&/ {++count}
        ' file1
) > newfile
Not pretty but it should work ...
# 3  
Old 06-16-2005
Computer To be contiued: replace a paragraph in a textfile

Thanks, Kemisola. It works almost. The new paragraph is inserted.
However the old one remains. As I include comments in your script,
I should be able to a way to delete the old paragraph. Smilie
# 4  
Old 06-17-2005
Instead of
if (count == 2) exit
should be
if (count == 1) exit


Also I think "substr" is not necessary. "^&&" marks the end of a section.
# 5  
Old 06-17-2005
In my previous posting I meant
/^&&/ {++count; ...
instead of /^&/ { ... substr ...}
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
Login or Register to Ask a Question