how to append a block of statements after another block in the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to append a block of statements after another block in the file
# 1  
Old 04-09-2009
how to append a block of statements after another block in the file

Hi
I need to append the following block of statements in the middle of the file:
#
openpipe tsdbdwn2
set -x
exec >> /tmp/tsdbdwn2.fifo 2>&1
#

This needs to be appended right after another block of statements:
if test $# -eq 0 ;then
echo "Safety check - do you really wish to run" $0 " on " `hostname` "?"
read safety
case $safety in
y|yes|Y|YES|Yes ) ;;
* ) echo $0: aborting ; exit 1 ;;
esac
fi


Is there an awk/sed commands which will allow me to do that, since I heed to update multiple files. Thanks a lot for any help.
# 2  
Old 04-09-2009
How to insert a block of statements within a set of files

The smart answer is "no there is no easy way to do this. Edit the files yourself".

However, I took it as a challenge and came up with a solution, such as it is.
It will insert the new code multiple times, if necessary.

It's possible to get the wrong result if there is a lot of duplicate code in the file
and the lines follow each other with no breaks of any kind.
This is unlikely.

First, you need a file with the lines you want to insert .
newcode file:
#
Code:
openpipe tsdbdwn2
set -x
exec >> /tmp/tsdbdwn2.fifo 2>&1
#

You need a file containing the block of code which needs to be followed by the new code .
codepattern file:

Code:
if test $# -eq 0 ;then
echo "Safety check - do you really wish to run" $0 " on " `hostname` "?"
read safety
case $safety in
y|yes|Y|YES|Yes ) ;;
* ) echo $0: aborting ; exit 1 ;;
esac
fi


And you need this script which finds sequences of matches against the code patterns
and generates sed commands to insert the new code wherever it is needed.

Code:
#!/bin/sh


case "$1" in "" )
        echo usage : $0 newcode_file existingblock_file  inputfile... 1>&2
        exit 0
        ;;
esac


newcodefile=$1
patternfile=$2
shift
shift
patternlines=`cat $patternfile | wc -l`
patternlines=`echo $patternlines`


for infile in "$@"
do

  # get list of matches for patterns, with line numbers
  fgrep -n -f $patternfile $infile  | \
  nawk -F: -v seqlen=$patternlines '
     #{ print "last=" last " seq=" seq " f1=" $1 "  " $2 ;}

    last == "" { last = $1 ; next }     # initialize

    {
        # check for match with sequential line number
        if ($1 == (last + 1) ) {
                # sequential match; check length of sequence
                if  (++seq == seqlen) {
                        # "add after " $1 " : " $0;
                        print $1        # print line number of
                                        # end of code block matched
                }
        } else {
                # not sequential any more - start over
                seq = 1         # first match in sequence
        }
    }

    { last = $1}
    ' - | (
                echo sed -e \'\' '\c'
                while read line
                do
                # output sed command to read new code into file
                        echo "-e '${line}r $newcodefile' \c"
                done
                echo "$infile > $infile.new"
        )
done

The output is a set of sed commands, which you can put in a file as a script,
or just pipe to "sh -x " to see them run.

Code:
$ ./insertcode
usage : ./insertcode newcode_file existingblock_file inputfile...
  

$ ./insertcode newcode codepattern f1 f2 f3
sed -e '' -e '20r newcode' f1 > f1.new
sed -e '' -e '19r newcode' f2 > f2.new
sed -e '' -e '20r newcode' -e '30r newcode' -e '49r newcode' f3 > f3.new

Code:
$ sed -e '' -e '20r newcode' f1 > f1.new
$ diff f1 f1.new
20a21,25
> #
> openpipe tsdbdwn2
> set -x
> exec >> /tmp/tsdbdwn2.fifo 2>&1
> #

# 3  
Old 04-09-2009
awesome solution, nobody8.

i'll present my solution, too, but just cause it's an interesting paradigm
that can be modified to do almost anything.

Code:
for file in `cat my_list_of_files_to_edit` ; do

vi +/"Safety check - do you really wish to run" $file << EOF
:/^fi
O#
openpipe tsdbdwn2
set -x
exec >> /tmp/tsdbdwn2.fifo 2>&1
#
^[:wq
EOF

done

where ^[ is actually the ESCAPE character.
# 4  
Old 04-10-2009
Thanks a lot guys!!!! This is what i need Smilie
# 5  
Old 04-11-2009
Simpler is better.

Quote:
Originally Posted by quirkasaurus
awesome solution, nobody8.

i'll present my solution, too, but just cause it's an interesting paradigm
that can be modified to do almost anything.
Indeed - I like this a lot.
This is an excellent approach if the files to be edited have a unique and easily found
insertion point and it is safe to stuff in the change.

Simpler is better.
# 6  
Old 04-13-2009
thanks, nobody.

Last edited by quirkasaurus; 04-13-2009 at 11:37 AM.. Reason: oops - - - - stupid correction was wrong!!!!@!@!@ sorry.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Add a block of code at the end of a specific block

I need to search for a block with the starting pattern say "tabId": "table_1", and ending pattern say "]" and then add a few lines before "]" "block1":"block_111" "tabId": "table_1", "title":"My title" ..... .... }] how do I achieve it using awk and sed. Thanks, Lakshmi (3 Replies)
Discussion started by: Lakshmikumari
3 Replies

2. Shell Programming and Scripting

Get values block by block in same file

I have a file say "SAMPLE.txt" with following content, P1 10,9:6/123456 P2 blah blah P1 10,9:5/98765 P2 blah blah P1 blah blah P2 I want a output file say "RESULT.txt" as, Value1:123456 Value2:98765 Value3:NULL (17 Replies)
Discussion started by: garvit184
17 Replies

3. Shell Programming and Scripting

How to append to array within conditional block in ksh/korn shell?

Hi, I have one array created and some values are there in ksh. I want to append some other values to it based on some condition in if statement. #!/bin/ksh echo "---------------------------------------------------" set -A ipaddr_arr $(egrep -v '^#|^::|^$' /etc/hosts |awk '{print $1}'... (2 Replies)
Discussion started by: sanzee007
2 Replies

4. Shell Programming and Scripting

sed - append line after block

Hi, I posted in another section, but no reply yet. I have an ini file with sections denoted as follows (for example) blah=blah blee=blee bloo=bloo blur=blur blaa=blaa I have ksh script that needs to append a line ${line} to the end of section ${section} I saw this... (7 Replies)
Discussion started by: andyatit
7 Replies

5. Shell Programming and Scripting

Printing a block of lines from a file, if that block does not contain two patterns using sed

I want to process a file block by block using sed, and if that block does not contain two patterns, then that complete block has to be printed. See below for the example data. ................................server 1............................... running process 1 running... (8 Replies)
Discussion started by: Kesavan
8 Replies

6. UNIX for Advanced & Expert Users

Move a block of lines to file if string found in the block.

I have a "main" file which has blocks of data for each user defined by tags BEGIN and END. BEGIN ID_NUM:24879 USER:abc123 HOW:47M CMD1:xyz1 CMD2:arp2 STATE:active PROCESS:id60 END BEGIN ID_NUM:24880 USER:def123 HOW:4M CMD1:xyz1 CMD2:xyz2 STATE:running PROCESS:id64 END (7 Replies)
Discussion started by: grep_me
7 Replies

7. Shell Programming and Scripting

Grepping text block by block by using for loop

Hei buddies, Need ur help once again. I have a file which has bunch of lines which starts from a fixed pattern and ends with another fixed pattern. I want to make use of these fixed starting and ending patterns to select the bunch, one at a time. The input file is as follows. Hi welcome... (12 Replies)
Discussion started by: anushree.a
12 Replies

8. UNIX for Advanced & Expert Users

Deciding whether to get a buffer cache block or inode block

I was reading a book on UNIX internals "The design of the UNIX Operating system." There are two memory structures that are confusing me: 1) Buffer cache 2) Inode cache My questions are 1) Does a process get both buffer cache and Indoe cache allocated when it opens/creates a file? 2) if no,... (1 Reply)
Discussion started by: sreeharshasn
1 Replies

9. Shell Programming and Scripting

finding a block in a file and replace with another file block.

(1) Yes but how is this block different from the other 24? You will need this information in order to identify and replace this block correctly (out of the 25). Ans: The 1st line and last line of this block are unique from other block. The 1st line is “rem Subset Rows (&&tempName.*) and The... (1 Reply)
Discussion started by: Zaheer.mic
1 Replies

10. Shell Programming and Scripting

block of name value pair to db insert statements

Hi, I need to convert the following file into DB insert statements. $ cat input.txt START name=john id=123 date=12/1/09 END START name=sam id=4234 status=resigned date=12/1/08 END (2 Replies)
Discussion started by: vlinet
2 Replies
Login or Register to Ask a Question