Conditional replace after reading in a file


 
Thread Tools Search this Thread
Top Forums Programming Conditional replace after reading in a file
# 1  
Old 02-27-2012
Conditional replace after reading in a file

I need to read the contents of a file. Then I need to grep for a keyword and replace part of the grepped line based on the condition of previous and present line.


Example input file:
Code:
  V { 
    port1       = P;
    port2       = 0;
    shift_port  = P0; /* if next shift_port is P0 I need to replace this with M0; and next shift_port's P0; with 00;
    port3       = 0;
    reset       = 1;
   }
  W wf_cyc;
  V { 
    port1       = P;
    port2       = 0;
    shift_port  = P0;
    port3       = 0;
    reset       = 1;
   }
  Macro "Inputs" { 
    test_ip =
        110111010110
  }

  W wf_cyc;
  V { 
    port1       = P;
    port2       = 0;
    shift_port  = 00;
    port3       = 0;
    reset       = 1;
  }
  W wf_cyc;
  V { 
    port1       = P;
    port2       = 0;
    shift_port  = 00; 
    port3       = 0;
    reset       = 1;
   }
  W wf_cyc;
  V { 
    port1       = P;
    port2       = 0;
    shift_port  = P0;/* if next shift_port is P0; I need to replace this with M0; and next shift_port's P0 with 00;
    port3       = 0;
    reset       = 1;
   }
  W wf_cyc;
  V { 
    port1       = P;
    port2       = 0;
    shift_port  = P0;/* if next shift_port is P0; I need to replace this with M0; and next shift_port's P0 with 00;
    port3       = 0;
    reset       = 1;
   }
}

Desired output file
Code:
  V { 
    port1       = P;
    port2       = 0;
    shift_port  = M0; /* if next shift_port is P0; I need to replace this with M0; and next shift_port's P0 with 00;
    port3       = 0;
    reset       = 1;
   }
  W wf_cyc;
  V { 
    port1       = P;
    port2       = 0;
    shift_port  = 00;
    port3       = 0;
    reset       = 1;
   }
  Macro "Inputs" { 
    test_ip =
        110111010110
  }

  W wf_cyc;
  V { 
    port1       = P;
    port2       = 0;
    shift_port  = 00;
    port3       = 0;
    reset       = 1;
  }
  W wf_cyc;
  V { 
    port1       = P;
    port2       = 0;
    shift_port  = 00; 
    port3       = 0;
    reset       = 1;
   }
  W wf_cyc;
  V { 
    port1       = P;
    port2       = 0;
    shift_port  = M0;/* if next shift_port is P0; I need to replace this with M0; and next shift_port's P0 with 00;
    port3       = 0;
    reset       = 1;
   }
  W wf_cyc;
  V { 
    port1       = P;
    port2       = 0;
    shift_port  = 00;/* if next shift_port is P0; I need to replace this with M0; and next shift_port's P0 with 00;
    port3       = 0;
    reset       = 1;
   }
}

I am trying to use a perl script to implement this.

Thanks in advance for your help
# 2  
Old 02-27-2012
Code:
$ cat shiftport.awk

/shift_port/ {
        # Do some matching to get the value of shift_port
        split($0, A, ";");      # Split on ; to get statement
        N=split(A[1], A, "=");  # Split on = to get vars
        gsub(/ /, "", A[N]);    # Get rid of spaces

        if((PREV_PORT == "P0") && (A[N] == "P0"))
        {
                # Substitute 00 for P0 for this line
                sub(A[N], "00");
                # Substitute M0 for P0 in previous line
                sub("P0", "M0", LINE[PREV_LINE]);
                # Tell ourselves that this line is 00, not P0
                A[N]="00"
        }

        # Save for later, so we can check and replace previous line
        PREV_PORT=A[N];
        PREV_LINE=L+1;
}

# Save lines for substitution and printing later
{       LINE[++L]=$0    }

# Print all lines
END {   for(N=1; N<=L; N++)     print LINE[N];  }


$ awk -f shiftport.awk data

  V {
    port1       = P;
    port2       = 0;
    shift_port  = M0; /* if next shift_port is P0 I need to replace this with M0; and next shift_port's P0; with 00;
    port3       = 0;
    reset       = 1;
   }
  W wf_cyc;
  V {
    port1       = P;
    port2       = 0;
    shift_port  = 00;
    port3       = 0;
    reset       = 1;
   }
  Macro "Inputs" {
    test_ip =
        110111010110
  }

  W wf_cyc;
  V {
    port1       = P;
    port2       = 0;
    shift_port  = 00;
    port3       = 0;
    reset       = 1;
  }
  W wf_cyc;
  V {
    port1       = P;
    port2       = 0;
    shift_port  = 00;
    port3       = 0;
    reset       = 1;
   }
  W wf_cyc;
  V {
    port1       = P;
    port2       = 0;
    shift_port  = M0;/* if next shift_port is P0; I need to replace this with M0; and next shift_port's P0 with 00;
    port3       = 0;
    reset       = 1;
   }
  W wf_cyc;
  V {
    port1       = P;
    port2       = 0;
    shift_port  = 00;/* if next shift_port is P0; I need to replace this with M0; and next shift_port's P0 with 00;
    port3       = 0;
    reset       = 1;
   }
}

$

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 02-27-2012
Awesome. ! it works perfect.
Thanks for you quick reply

---------- Post updated at 04:49 PM ---------- Previous update was at 08:45 AM ----------

Hi Corona,

I need another small improvement in the code

Say the input file is the one below. I need to replace 1st P0 with M0 and second with 00 if and only if the value of port is 1 on both V{}

Say the input file is the one below. I need to replace 1st P0 with M0 and second with 00 if and only if the value of port3 is not 1 on both V{}

Input file

Code:
V { 
    port1       = P;
    port2       = 0;
    shift_port  = P0; 
    port3       = 1; /* port 3 is 1 . So dont replace this even if next shift port is P0 */
    reset       = 1;
   }
  W wf_cyc;
  V { 
    port1       = P;
    port2       = 0;
    shift_port  = P0; /* port 3 is 0 and current shift_port is P0. If port 3 is 0 in next v{} and shift_port is P0 then replace this with M0 and next shift port with 00 */
    port3       = 0;
    reset       = 1;
   }

 W wf_cyc;
  V { 
    port1       = P;
    port2       = 0;
    shift_port  = P0;
    port3       = 0;
    reset       = 1;
   }

  Macro "Inputs" { 
    test_ip =
        110111010110
  }

  W wf_cyc;
  V { 
    port1       = P;
    port2       = 0;
    shift_port  = P0;
    port3       = 1; /* port 3 is 1 . So dont replace this even if next shift port is P0 */
    reset       = 1;
  }
  W wf_cyc;
  V { 
    port1       = P;
    port2       = 0;
    shift_port  = 00; 
    port3       = 1;
    reset       = 1;
   }
  W wf_cyc;
  V { 
    port1       = P;
    port2       = 0;
    shift_port  = P0;
    port3       = 1;  /* port 3 is 1 . So dont replace this even if next shift port is P0 */
    reset       = 1;
   }
  W wf_cyc;
  V { 
    port1       = P;
    port2       = 0;
    shift_port  = P0; /* port 3 is 0 and current shift_port is P0. If port 3 is 0 in next v{} and shift_port is P0 then replace this with M0 and next shift port with 00 */
    port3       = 0;
    reset       = 1;
   }
 W wf_cyc;
  V { 
    port1       = P;
    port2       = 0;
    shift_port  = P0; 
    port3       = 0;
    reset       = 1;
   }
  V { 
    port1       = P;
    port2       = 0;
    shift_port  = P0;
    port3       = 1;  /* port 3 is 1 . So dont replace this even if next shift port is P0 */
    reset       = 1;
   }
  V { 
    port1       = P;
    port2       = 0;
    shift_port  = P0;
    port3       = 1;  /* port 3 is 1 . So dont replace this even if next shift port is P0 */
    reset       = 1;
   }
}

Required output

Code:
V { 
    port1       = P;
    port2       = 0;
    shift_port  = P0; 
    port3       = 1; /* port 3 is 1 . So dont replace this even if next shift port is P0 */
    reset       = 1;
   }
  W wf_cyc;
  V { 
    port1       = P;
    port2       = 0;
    shift_port  = M0; /* port 3 is 0 and current shift_port is P0. If port 3 is 0 in next v{} and shift_port is P0 then replace this with M0 and next shift port with 00 */
    port3       = 0;
    reset       = 1;
   }

 W wf_cyc;
  V { 
    port1       = P;
    port2       = 0;
    shift_port  = 00;
    port3       = 0;
    reset       = 1;
   }

  Macro "Inputs" { 
    test_ip =
        110111010110
  }

  W wf_cyc;
  V { 
    port1       = P;
    port2       = 0;
    shift_port  = P0;
    port3       = 1; /* port 3 is 1 . So dont replace this even if next shift port is P0 */
    reset       = 1;
  }
  W wf_cyc;
  V { 
    port1       = P;
    port2       = 0;
    shift_port  = 00; 
    port3       = 1;
    reset       = 1;
   }
  W wf_cyc;
  V { 
    port1       = P;
    port2       = 0;
    shift_port  = P0;
    port3       = 1;  /* port 3 is 1 . So dont replace this even if next shift port is P0 */
    reset       = 1;
   }
  W wf_cyc;
  V { 
    port1       = P;
    port2       = 0;
    shift_port  = M0; /* port 3 is 0 and current shift_port is P0. If port 3 is 0 in next v{} and shift_port is P0 then replace this with M0 and next shift port with 00 */
    port3       = 0;
    reset       = 1;
   }
 W wf_cyc;
  V { 
    port1       = P;
    port2       = 0;
    shift_port  = 00; 
    port3       = 0;
    reset       = 1;
   }
  V { 
    port1       = P;
    port2       = 0;
    shift_port  = P0;
    port3       = 1;  /* port 3 is 1 . So dont replace this even if next shift port is P0 */
    reset       = 1;
   }
  V { 
    port1       = P;
    port2       = 0;
    shift_port  = P0;
    port3       = 1;  /* port 3 is 1 . So dont replace this even if next shift port is P0 */
    reset       = 1;
   }
}


Last edited by naveen@; 02-28-2012 at 11:51 AM..
# 4  
Old 02-28-2012
which port? port1, port2, or port3?
This User Gave Thanks to Corona688 For This Post:
# 5  
Old 02-28-2012
Hi Corona688,

Sorry about the confusion and lack of clarity
If port3 is 1 i wouldnt want to replace current and next line. Only if port3 is 0 i would like to replace current P0 with M0 and next P0 with 00 (provided in the next v{} the port3 is again not 1)

---------- Post updated at 07:56 AM ---------- Previous update was at 07:55 AM ----------

I edited the snippet above to make the problem clear with inline comments
# 6  
Old 02-28-2012
Code:
$ cat shiftport2.awk

BEGIN   {       C=1     }

# Save all lines for printing/substitution later
{       LINE[NR]=$0     }

/[{}]/ {        # Count brackets to find code blocks
        STR=$0
        sub(/\/[*].*/, "", STR);        # Delete comments
        while(sub(/{/, "", STR))        BRACKET++;
        while(sub(/}/, "", STR))
        {
                if(V["shift_port",C])   BEND[C++]=NR

                BRACKET--;
        }
}

/[^=;]+=[^=;]+;/ {      # Pick up any values within a code block
        # Do some matching to get the value of port3
        split($0, A, ";");      # Split on ; to get statement
        N=split(A[1], A, "=");  # Split on = to get vars

        VAL=A[N];       KEY=A[1]

        gsub(/ /, "", KEY);     # Get rid of spaces
        gsub(/ /, "", VAL);

        V[KEY,C]=VAL    # Save value for later
        VL[KEY,C]=NR    # Save line number of value for later
}

END {
        # Hunt for patterns and substitute
        for(N=1; N<C; N++)
        if(     (V["shift_port",N]      ==      "P0") &&
                (V["shift_port",N+1]    ==      "P0") &&
                (V["port3",N]           !=      1) &&
                (V["port3",N+1]         !=      1)      )
        {
                # Replace in stored lines
                sub(/P0/, "M0", LINE[VL["shift_port",N]]);
                sub(/P0/, "00", LINE[VL["shift_port",N+1]]);
                # Replace in stored data, so we don't re-match the next P0 later
                V["shift_port",N]="M0";
                V["shift_port",N+1]="00";
        }

        for(N=1; N<=NR; N++)    print LINE[N];
}

$ awk -f shiftport2.awk data

V {
    port1       = P;
    port2       = 0;
    shift_port  = P0;
    port3       = 1; /* port 3 is 1 . So dont replace this even if next shift port is P0 */
    reset       = 1;
   }
  W wf_cyc;
  V {
    port1       = P;
    port2       = 0;
    shift_port  = M0; /* port 3 is 0 and current shift_port is P0. If port 3 is 0 in next v{} and shift_port is P0 then replace this with M0 and next shift port with 00 */
    port3       = 0;
    reset       = 1;
   }

 W wf_cyc;
  V {
    port1       = P;
    port2       = 0;
    shift_port  = 00;
    port3       = 0;
    reset       = 1;
   }

  Macro "Inputs" {
    test_ip =
        110111010110
  }

  W wf_cyc;
  V {
    port1       = P;
    port2       = 0;
    shift_port  = P0;
    port3       = 1; /* port 3 is 1 . So dont replace this even if next shift port is P0 */
    reset       = 1;
  }
  W wf_cyc;
  V {
    port1       = P;
    port2       = 0;
    shift_port  = 00;
    port3       = 1;
    reset       = 1;
   }
  W wf_cyc;
  V {
    port1       = P;
    port2       = 0;
    shift_port  = P0;
    port3       = 1;  /* port 3 is 1 . So dont replace this even if next shift port is P0 */
    reset       = 1;
   }
  W wf_cyc;
  V {
    port1       = P;
    port2       = 0;
    shift_port  = M0; /* port 3 is 0 and current shift_port is P0. If port 3 is 0 in next v{} and shift_port is P0 then replace this with M0 and next shift port with 00 */
    port3       = 0;
    reset       = 1;
   }
 W wf_cyc;
  V {
    port1       = P;
    port2       = 0;
    shift_port  = 00;
    port3       = 0;
    reset       = 1;
   }
  V {
    port1       = P;
    port2       = 0;
    shift_port  = P0;
    port3       = 1;  /* port 3 is 1 . So dont replace this even if next shift port is P0 */
    reset       = 1;
   }
  V {
    port1       = P;
    port2       = 0;
    shift_port  = P0;
    port3       = 1;  /* port 3 is 1 . So dont replace this even if next shift port is P0 */
    reset       = 1;
   }
}

$

This User Gave Thanks to Corona688 For This Post:
# 7  
Old 02-28-2012
Thanks Corona,
This isnt a homework problem. I wanted to edit some vectors at work and was struggling so seeked your help
Sorry abt the duplicate post
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace query by reading the file

Hi Guys, I am having below file which holds data like this file.txt name,id,flag apple,1,Y apple,2,N mango,1,Y mango,2,Y I need to read the above file and frame a query like this hive -s -e "create apple_view as select 1 from main_table;" hive -s -e "create mango_view as select... (11 Replies)
Discussion started by: rohit_shinez
11 Replies

2. Emergency UNIX and Linux Support

sed replace file contents by reading from another file

Hello, My input file1 is like this by tab-delimited chr1 mm10_knownGene stop_codon 3216022 3216024 0.000000 - . gene_id "uc007aeu.1"; transcript_id "uc007aeu.1"; chr1 mm10_knownGene CDS 3216025 3216968 0.000000 - 2 gene_id "uc007aeu.1"; transcript_id "uc007aeu.1"; ... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

3. Shell Programming and Scripting

sed conditional \n replace for each line

How could be removed \n only if appearing at position 80 in the line? (4 Replies)
Discussion started by: RomanF
4 Replies

4. Shell Programming and Scripting

Search & Replace in Multiple Files by reading a input file

I have a environment property file which contains: Input file: value1 = url1 value2 = url2 value3 = url3 and so on. I need to search all *.xml files under directory for value1 and replace it with url1. Same thing I have to do for all values mentioned in input file. I need script in unix bash... (7 Replies)
Discussion started by: Shamkamde
7 Replies

5. Shell Programming and Scripting

Conditional Search/Replace

I'm looking for an awk or (preferably) sed solution to search a pipe delimited file for any occurrence of an email address that does not include a designed domain, and replace the email address with a blank. E.g. hello|smith@designateddomain.com|jones@anotherdomain.edu|1234| turns into: ... (2 Replies)
Discussion started by: tiggyboo
2 Replies

6. Shell Programming and Scripting

Sed conditional replace

Given this row: |lastname1|middlename1|firstname1|lastname2|middlename2|firstname2 produce this result: |lastname|middlename|firstname where the resultant names are based on the presence of the #2 names above. I.e., if a #2 name is passed (usually will be null,) use that - otherwise... (8 Replies)
Discussion started by: tiggyboo
8 Replies

7. Shell Programming and Scripting

Search & Replace in Multiple Files by reading a input file

Hi, I have a folder which contains multiple config.xml files and one input file, Please see the below format. Config Files format looks like :- Code: <application name="SAMPLE-ARCHIVE"> <NVPairs name="Global Variables"> <NameValuePair> ... (0 Replies)
Discussion started by: haiksuresh
0 Replies

8. Shell Programming and Scripting

Replace a column with a value conditional on a value in col1

Hi, Perhaps a rather simple problem...? I have data that looks like this. BPC0013 ANNUL_49610 0 0 1 1 BPC0014 ANNUL_49642 0 0 2 1 BPC0015 ANNUL_49580 0 0 1 1 BPC0016 ANNUL_49596 0 0 2 1 BPC0017 VULGO_49612 0 0 1 1 BPC0018 ANNUL_49628 0 0 1 1 BPC0019 ANNUL_49692 0 0 2 1 170291_HMG... (4 Replies)
Discussion started by: genehunter
4 Replies

9. Shell Programming and Scripting

awk help to do conditional find and replace

Hi, I have a Line input for awk as follows DROP MATERIALIZED VIEW MCR.COMM_STACK; CREATE MATERIALIZED VIEW "MCR"."COMM_STACK" ON PREBUILT TABLE WITHOUT REDUCED PRECISION USING INDEX REFRESH FAST ON DEMAND START WITH sysdate+0 NEXT SYSDATE + 7 WITH PRIMARY KEY USING DEFAULT... (3 Replies)
Discussion started by: rajan_san
3 Replies

10. Shell Programming and Scripting

sed conditional string replace for each line

Hi all, I appreciate the enormous amount of knowledge that flows in this forum. I am an average UNIX user. I have many files with lines like the below. I have separated each line with space for ease of reading. I need to replace the first occurance of "/00" with null on those lines that have... (6 Replies)
Discussion started by: Nanu_Manju
6 Replies
Login or Register to Ask a Question