Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-07-2012
Registered User
 

Join Date: Apr 2011
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
modify a file by inserting a conditional values

Hi,
input file

Code:
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

Code:
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!!!

Last edited by Franklin52; 02-07-2012 at 03:00 AM.. Reason: Please use code tags for code and data samples, thank you
Sponsored Links
    #2  
Old 02-07-2012
balajesuri's Avatar
#! /bin/bash
 

Join Date: Apr 2009
Location: India
Posts: 1,019
Thanks: 9
Thanked 285 Times in 277 Posts

Code:
#! /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;


Code:
$ ./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.
http://www.unix.com/shell-programmin...ent-files.html
http://www.unix.com/shell-programmin...ent-files.html
http://www.unix.com/shell-programmin...ern-found.html

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

Last edited by balajesuri; 02-07-2012 at 04:06 AM..
Sponsored Links
    #3  
Old 02-07-2012
Registered User
 

Join Date: Feb 2012
Location: India
Posts: 102
Thanks: 9
Thanked 2 Times in 2 Posts

Code:
#!/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

Code:
# 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

Last edited by Franklin52; 02-07-2012 at 05:49 AM.. Reason: Please use code tags for code and data samples, thank you
    #4  
Old 02-07-2012
Scrutinizer's Avatar
mother ate her
 

Join Date: Nov 2008
Location: Amsterdam
Posts: 5,371
Thanks: 80
Thanked 1,107 Times in 1,011 Posts
awk:

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


Last edited by Scrutinizer; 02-07-2012 at 06:23 AM..
Sponsored Links
    #5  
Old 02-07-2012
Registered User
 

Join Date: Apr 2011
Posts: 39
Thanks: 0
Thanked 0 Times in 0 Posts
thanks a lot all of you......

gr8 brains!!!!
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perform Operations on One File Conditional on Data in Another File hydrabane Shell Programming and Scripting 2 09-20-2011 05:58 PM
conditional Testing numeric and text values problems bassmasta1 Shell Programming and Scripting 2 06-01-2011 06:07 AM
remove values of a file one by one from 2nd file and then print the remaining values of 2nd file AshwaniSharma09 Shell Programming and Scripting 5 11-12-2010 03:23 AM
help in inserting values in date format ali560045 Shell Programming and Scripting 3 02-04-2008 09:12 AM
Inserting Values From A File Into A Table ragha81 Shell Programming and Scripting 2 09-16-2006 07:10 PM



All times are GMT -4. The time now is 04:31 AM.