modify a file by inserting a conditional values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting modify a file by inserting a conditional values
# 1  
Old 02-07-2012
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 04:00 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 02-07-2012
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.
https://www.unix.com/shell-programmin...ent-files.html
https://www.unix.com/shell-programmin...ent-files.html
https://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 05:06 AM..
# 3  
Old 02-07-2012
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 06:49 AM.. Reason: Please use code tags for code and data samples, thank you
# 4  
Old 02-07-2012
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 07:23 AM..
# 5  
Old 02-07-2012
thanks a lot all of you......

gr8 brains!!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Inserting values to database

hi , I'm new to Unix shell scripting. I need help to insert read csv which has two columns -emp no and logged date. csv file is a large file so i want to keep the insertion query in a separate .sql file. csv file looks this: empno | loggeddate ___________________ 5666 ,... (5 Replies)
Discussion started by: preema
5 Replies

2. Shell Programming and Scripting

Inserting values into database from an excel

Hi, I have a requirement where I have an excel sheet with the below values COL1 COL2 COL3 Germany URGENT NORMAL I want to cut the values of this excel in such a way that I get the values and pass it to an insert statement update tbfin set... (2 Replies)
Discussion started by: venkidhadha
2 Replies

3. Shell Programming and Scripting

Pattern search and modify the values

I have one file and the file may contain 500 to 15,000 records. I need to search pattern ^F509= and then increment the corresponding value by one and print the entire line. Please note that Its not a fixed length file. Can anyone please help? ex: ^F509=204656 ^F509=204656 ... (6 Replies)
Discussion started by: vinus
6 Replies

4. Shell Programming and Scripting

Modify existing values in a file

Hi, I am trying to write a shell script which will modify existing values and take backup of old values. file trying to modify is : /etc/sysctl.conf script name: modify.sh execute ./modify.sh enter ref no: 123 add below values in file sysctl.cnf kernel.shmall = 4194304 ... (7 Replies)
Discussion started by: darling
7 Replies

5. Shell Programming and Scripting

Problem in inserting values of variable of shell

hi all, i have one shell script like this #!/bin/bash -xv ENV_NAME=`cat $IB_HOME_DIR/cfg/ibProfile.sh | grep "RDM_CONN" | cut -f 2 -d "@"` CURRENT_DIR=`pwd`; string=$IB_HOME_DIR string1="$string/FRGFOLDER/input" #sed "s/string3/$string1" frg_event_src.sql > modifiedinsert.sql sqlplus... (2 Replies)
Discussion started by: ramsavi
2 Replies

6. Shell Programming and Scripting

Inserting variable values in filename

Hi All, I have a directory containing multiple files. and also a txt file which contains the list of all filenames and certain values. I want to read the text file line by line and if its 2nd column is matched with the filename in directory, then it must insert the values in 7th column to... (14 Replies)
Discussion started by: CAch
14 Replies

7. Shell Programming and Scripting

Perform Operations on One File Conditional on Data in Another File

Hello all, I am looking for a solution to the following problem. Perl or python solutions also welcome. Given this input: And this input: I want to get this output. The rule being that if the number in the first file is < 0.9, then the corresponding two columns on... (2 Replies)
Discussion started by: hydrabane
2 Replies

8. Shell Programming and Scripting

conditional Testing numeric and text values problems

hi, when i type in a value higher than 20 it will show me the validation but if i put a negative figure it just goes goes blank where i just keep typing and nothing happens so i had to control c to exit out the process, can someone help me please. clear read -p "please enter... (2 Replies)
Discussion started by: bassmasta1
2 Replies

9. Shell Programming and Scripting

help in inserting values in date format

how to assign values in this date format a=`date +"%H%M%S"` how to give value of H=22,here in this format so that i can grep 22nd hour.Below is the script -------------------------------------------------------------------------- a=`date +"%H%M%S"` for i in *.log do cat $i | grep $a... (3 Replies)
Discussion started by: ali560045
3 Replies

10. Shell Programming and Scripting

Inserting Values From A File Into A Table

Guys, I want to insert some values from a log file into a temporary table. the values in the log file looks like this SV_NAME CC_NAME CP_DISP_RUN_STATE ------- ------------------- ----------------- sble01 ALLTEL WorkMon24Hrs Running I want to enter the... (2 Replies)
Discussion started by: ragha81
2 Replies
Login or Register to Ask a Question