modify a file by inserting a conditional values

Hi,
input file

CCCC                 1204     215764.85   9405410.40               1189      
DDDD                 4498 1503 4617 1507 4723 1517 4829 1528 4996 1540
DDDD                 5199 1556 5278 1567 5529 1603 5674 1614 6076 1915
DDDD                 6605 2371 7004 2779
CCCC                 1284     216035.45   9405830.95               1216      
DDDD                 4498 1503 4626 1505 4720 1515 4832 1529 4962 1537
DDDD                 5270 1569 5597 1596 5671 1607 5828 1694 6115 1933
DDDD                 6392 2165 7004 2810

Output file should be

CCCC                 1204     215764.85   9405410.40               1189      
DDDD                      0 1500 4498 1503 4617 1507 4723 1517 4829 1528
DDDD                 4996 1540 5199 1556 5278 1567 5529 1603 5674 1614
DDDD                 6076 1915 6605 2371 7004 2779
CCCC                 1284     216035.45   9405830.95               1216      
DDDD                      0 1500 4498 1503 4626 1505 4720 1515 4832 1529
DDDD                 4962 1537 5270 1569 5597 1596 5671 1607 5828 1694
DDDD                 6115 1933 6392 2165 7004 2810

for DDDD row just after CCCC line the $2 is 0 and $ 3 is 1500 and accordingly there is a shift in the output..

PLz help...any brilliant script for this.
regards

---------- Post updated 02-07-12 at 12:13 AM ---------- Previous update was 02-06-12 at 11:34 PM ----------

any help!!!

---------- Post updated at 12:13 AM ---------- Previous update was at 12:13 AM ----------

any help!!!

#! /usr/bin/perl -w
use strict;

my (@x, @y, $l1, $l2);

open I, "< inputfile";
for (<I>) {
    if (/^CCCC/) {
        print;
        ($l1, $l2) = ("   0", 1500);
    }
    if (/^DDDD/) {
        @x = split /\s+/;
        print shift @x , " " x 17;
        unshift (@x, $l1, $l2);
        if (@x > 10) { $l2 = pop @x; $l1 = pop @x; }
        print "@x\n";
    }
}
close I;
$ ./test.pl
CCCC                 1204     215764.85   9405410.40               1189
DDDD                    0 1500 4498 1503 4617 1507 4723 1517 4829 1528
DDDD                 4996 1540 5199 1556 5278 1567 5529 1603 5674 1614
DDDD                 6076 1915 6605 2371 7004 2779
CCCC                 1284     216035.45   9405830.95               1216
DDDD                    0 1500 4498 1503 4626 1505 4720 1515 4832 1529
DDDD                 4962 1537 5270 1569 5597 1596 5671 1607 5828 1694
DDDD                 6115 1933 6392 2165 7004 2810
  1. Please don't bump posts.

  2. I see that in many posts, you just don't bother to comment or thank on the solutions provided by members.
    merge two different files
    summing from two different files
    remove anything after repeated string pattern found

  3. You might want to read this post:
    Flag that marks user so you can avoid them

#!/bin/sh
IFSB=$IFS
IFS=$'\n'
echo -en "Enter file name: "
read flen;
filenm=$flen;
##
for ln in `cat $filenm`
do
    a=$(echo $ln | awk '{print $1}')
    if [[ $a == "CCCC" ]]
    then
        a1="Y"
        echo $ln;
    else
       if [[ $a1 == "Y" ]]
       then
         echo -e $ln | awk '{printf $1 " 0 1500 "} {for (i=2; i<=NF; i++)  printf "%s ", $i } {printf "\n"}'

       else
         echo -e $ln;
       fi
          a1="N"
    fi
done > myoutput

O/P:
let input in file file1

# sh myscr1.sh
Enter file name: file1
# cat myoutput
CCCC 1204 215764.85 9405410.40 1189
DDDD 0 1500 4498 1503 4617 1507 4723 1517 4829 1528 4996 1540
DDDD 5199 1556 5278 1567 5529 1603 5674 1614 6076 1915
DDDD 6605 2371 7004 2779
CCCC 1284 216035.45 9405830.95 1216
DDDD 0 1500 4498 1503 4626 1505 4720 1515 4832 1529 4962 1537
DDDD 5270 1569 5597 1596 5671 1607 5828 1694 6115 1933
DDDD 6392 2165 7004 2810
[root@gvc-nagios Shirish@Shukla]# diff -s file1 myoutput
2c2
< DDDD 4498 1503 4617 1507 4723 1517 4829 1528 4996 1540
---
> DDDD 0 1500 4498 1503 4617 1507 4723 1517 4829 1528 4996 1540
6c6
< DDDD 4498 1503 4626 1505 4720 1515 4832 1529 4962 1537
---
> DDDD 0 1500 4498 1503 4626 1505 4720 1515 4832 1529 4962 1537

Please let me know if have any probs in above script

-- Shirish Shukla

awk:

awk '/^DDDD/{sub($2,p FS $2);p=$(NF-1) FS $NF;NF-=2}/^CCCC/{p="0 1500"}1' infile

thanks a lot all of you......

gr8 brains!!!!