# Problem with awk script

**URL:** <https://community.unix.com/t/problem-with-awk-script/240260>\
**Category:** Shell Programming and Scripting\
**Created:** [May 16, 2009, 1:53pm UTC](https://community.unix.com/t/problem-with-awk-script/240260 "2009-05-16T13:53:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kshitij](https://community.unix.com/letter_avatar/kshitij/32/6_449c533f586939b6e5f246632a46421f.png) [@kshitij](https://community.unix.com/u/kshitij)\
**Post date:** [May 16, 2009, 1:53pm UTC](https://community.unix.com/t/problem-with-awk-script/240260/1 "2009-05-16T13:53:17Z")

</div>

Hi  
Can anyone help me in this Problem

File1  
#########################

HOLI 123 AND ONE TWO THREE AMITABH SAMSUNG  
POLI AND TWO SENSE CRYING WING  
PPIN TBFLAG I B AND OROLE TB\_HOT=" DCT" TB\_CAT=" CAT" TC\_NOT=" AND"  
+PIN TB=" HOT" TB\_GATE=" KOT" TB\_LATE=" MAT" TC=LOT  
MAT DAT SAT GATE IS A NAND AND PLAN ON THE RIGHT INFORMATION

##########################

I need to write a awk script should match word PPIN TBKFLAG and then remove the Key words TB\_GATE=" KOT" TB\_LATE=" MAT" TC=LOT

Rest should be same

Final file 1 should be like this

######################

HOLI 123 AND ONE TWO THREE AMITABH SAMSUNG  
POLI AND TWO SENSE CRYING WING  
PPIN TBFLAG I B AND OROLE TB\_HOT=" DCT" TB\_CAT=" CAT" TC\_NOT=" AND"  
+PIN TB=" HOT"  
MAT DAT SAT GATE IS A NAND AND PLAN ON THE RIGHT INFORMATION

##############

Thanks and Regards  
Kshitij Kulshreshtha

---

<div class="post-metadata">

**Author:** ![TonyFullerMalv](https://community.unix.com/user_avatar/community.unix.com/tonyfullermalv/32/1090_2.png) [@TonyFullerMalv](https://community.unix.com/u/TonyFullerMalv)\
**Post date:** [May 16, 2009, 2:11pm UTC](https://community.unix.com/t/problem-with-awk-script/240260/2 "2009-05-16T14:11:11Z")

</div>

This assumes that:

```nohighlight
PPIN TBFLAG I B AND OROLE TB_HOT=" DCT" TB_CAT=" CAT" TC_NOT=" AND" +PIN TB=" HOT" TB_GATE=" KOT" TB_LATE=" MAT" TC=LOT

```

is all on one line

```nohighlight
sed '/^PPIN TBFLAG/s/TB_GATE=" KOT" TB_LATE=" MAT" TC=LOT//'

```

Which when it finds a line starting with PPIN TBFLAG it then substitutes:  
TB\_GATE=" KOT" TB\_LATE=" MAT" TC=LOT  
with nothing.

---

<div class="post-metadata">

**Author:** ![kshitij](https://community.unix.com/letter_avatar/kshitij/32/6_449c533f586939b6e5f246632a46421f.png) [@kshitij](https://community.unix.com/u/kshitij)\
**Post date:** [May 16, 2009, 3:53pm UTC](https://community.unix.com/t/problem-with-awk-script/240260/3 "2009-05-16T15:53:21Z")

</div>

Hi Thanks for the Reply

But the Code is coming in different lines as shown in the below code

1. HOLI 123 AND ONE TWO THREE AMITABH SAMSUNG
2. POLI AND TWO SENSE CRYING WING
3. PPIN TBFLAG I B AND OROLE TB\_HOT=" DCT" TB\_CAT=" CAT" TC\_NOT=" AND"
4. +PIN TB=" HOT" TB\_GATE=" KOT" TB\_LATE=" MAT" TC=LOT
5. MAT DAT SAT GATE IS A NAND AND PLAN ON THE RIGHT INFORMATION

The awk script should search PPIN TBFLAG and then remove the flags TB\_GATE=" KOT" TB\_LATE=" MAT" TC=LOT

The Final code should be like this :

1. HOLI 123 AND ONE TWO THREE AMITABH SAMSUNG
2. POLI AND TWO SENSE CRYING WING
3. PPIN TBFLAG I B AND OROLE TB\_HOT=" DCT" TB\_CAT=" CAT" TC\_NOT=" AND"
4. +PIN TB=" HOT"
5. MAT DAT SAT GATE IS A NAND AND PLAN ON THE RIGHT INFORMATION

Thanks and Regards  
Kshitij Kulshreshtha

---

<div class="post-metadata">

**Author:** ![durden\_tyler](https://community.unix.com/user_avatar/community.unix.com/durden_tyler/32/1450_2.png) [@durden\_tyler](https://community.unix.com/u/durden_tyler)\
**Post date:** [May 16, 2009, 7:50pm UTC](https://community.unix.com/t/problem-with-awk-script/240260/4 "2009-05-16T19:50:24Z")

</div>

Maybe something like this:

```nohighlight
$
$ cat -n input.txt
     1 HOLI 123 AND ONE TWO THREE AMITABH SAMSUNG
     2 POLI AND TWO SENSE CRYING WING
     3 PPIN TBFLAG I B AND OROLE TB_HOT=" DCT" TB_CAT=" CAT" TC_NOT=" AND"
     4 +PIN TB=" HOT" TB_GATE=" KOT" TB_LATE=" MAT" TC=LOT
     5 MAT DAT SAT GATE IS A NAND AND PLAN ON THE RIGHT INFORMATION
$
$ awk '/^PPIN TBFLAG/ {ln=NR}; {if ($0 ~ /.*TB_GATE/ && NR==ln+1) {sub(/ TB_GATE.*/,"")} print}' input.txt
HOLI 123 AND ONE TWO THREE AMITABH SAMSUNG
POLI AND TWO SENSE CRYING WING
PPIN TBFLAG I B AND OROLE TB_HOT=" DCT" TB_CAT=" CAT" TC_NOT=" AND"
+PIN TB=" HOT"
MAT DAT SAT GATE IS A NAND AND PLAN ON THE RIGHT INFORMATION
$
$

```

tyler\_durden

---

<div class="post-metadata">

**Author:** ![devtakh](https://community.unix.com/letter_avatar/devtakh/32/6_449c533f586939b6e5f246632a46421f.png) [@devtakh](https://community.unix.com/u/devtakh)\
**Post date:** [May 17, 2009, 1:11am UTC](https://community.unix.com/t/problem-with-awk-script/240260/5 "2009-05-17T01:11:38Z")

</div>

```nohighlight
sed '/PPIN TBFLAG/{n;s/TB_GATE=\" KOT\" TB_LATE=\" MAT\" TC=LOT//g}' file

```

or

```nohighlight
awk '/PPIN TBFLAG/{print;getline;gsub(/TB_GATE=" KOT" TB_LATE=" MAT" TC=LOT/,"");print;next}{print}' file

```

cheers,  
Devaraj Takhellambam

---

<div class="post-metadata">

**Author:** ![ghostdog74](https://community.unix.com/letter_avatar/ghostdog74/32/6_449c533f586939b6e5f246632a46421f.png) [@ghostdog74](https://community.unix.com/u/ghostdog74)\
**Post date:** [May 17, 2009, 1:44am UTC](https://community.unix.com/t/problem-with-awk-script/240260/6 "2009-05-17T01:44:05Z")

</div>

> [@kshitij](#):
>
> Hi Thanks for the Reply
> 
> But the Code is coming in different lines as shown in the below code
> 
> 1. HOLI 123 AND ONE TWO THREE AMITABH SAMSUNG
> 2. POLI AND TWO SENSE CRYING WING
> 3. PPIN TBFLAG I B AND OROLE TB\_HOT=" DCT" TB\_CAT=" CAT" TC\_NOT=" AND"
> 4. +PIN TB=" HOT" TB\_GATE=" KOT" TB\_LATE=" MAT" TC=LOT
> 5. MAT DAT SAT GATE IS A NAND AND PLAN ON THE RIGHT INFORMATION
> 
> The awk script should search PPIN TBFLAG and then remove the flags TB\_GATE=" KOT" TB\_LATE=" MAT" TC=LOT

well, if you really must search for PPIN first, i assume that you want to remove TB\_GATE and the rest only when PPIN is found.  
here's an alternative solution in Python, assuming TB\_GATE is followed by TB\_LATE and TC.

```plaintext
f=0
for line in open("file"):
    if "PPIN" in line:f=1
    if f and "TB_GATE" in line:
        s = line.index("TB_GATE")
        line=line[:s]
        f=0
    print line.strip()
            

```

output:

```plaintext
# more file
1. HOLI 123 AND ONE TWO THREE AMITABH SAMSUNG
2. POLI AND TWO SENSE CRYING WING
3. PPIN TBFLAG I B AND OROLE TB_HOT=" DCT" TB_CAT=" CAT" TC_NOT=" AND"
4. MAT DAT SAT GATE IS A NAND AND PLAN ON THE RIGHT INFORMATION
5. +PIN TB=" HOT" TB_GATE=" KOT" TB_LATE=" MAT" TC=LOT

# ./test.py
1. HOLI 123 AND ONE TWO THREE AMITABH SAMSUNG
2. POLI AND TWO SENSE CRYING WING
3. PPIN TBFLAG I B AND OROLE TB_HOT=" DCT" TB_CAT=" CAT" TC_NOT=" AND"
4. MAT DAT SAT GATE IS A NAND AND PLAN ON THE RIGHT INFORMATION
5. +PIN TB=" HOT"

```
