# Modification to awk command

**URL:** <https://community.unix.com/t/modification-to-awk-command/363061>\
**Category:** Shell Programming and Scripting\
**Created:** [October 18, 2016, 12:36am UTC](https://community.unix.com/t/modification-to-awk-command/363061 "2016-10-18T00:36:08Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![SkySmart](https://community.unix.com/letter_avatar/skysmart/32/6_449c533f586939b6e5f246632a46421f.png) [@SkySmart](https://community.unix.com/u/SkySmart)\
**Post date:** [October 18, 2016, 12:36am UTC](https://community.unix.com/t/modification-to-awk-command/363061/1 "2016-10-18T00:36:08Z")

</div>

i have a php file that has this:

php.code

```nohighlight
#!/usr/bin/php
<?php 
  phpinfo();
  hlight_file( __FILE__ );
?>

```

I want my awk code grab whatever is inbetween and including the "\<?php" and "?\>".

Then, it should scan all the entries between these two points. And if the entries between these two points contain the pattern "phpinfo" AND "hlight", i dont want it to output anything.

If it doesn't containing the aforementioned two patterns, then I want it to print everything between and including the "\<?php" and "?\>".

here's the awk code i'm currently using:

```nohighlight
awk '/phpinfo|hlight/{next}/<?php/,/?>/' php.code

```

---

<div class="post-metadata">

**Author:** ![RavinderSingh13](https://community.unix.com/letter_avatar/ravindersingh13/32/6_449c533f586939b6e5f246632a46421f.png) [@RavinderSingh13](https://community.unix.com/u/RavinderSingh13)\
**Post date:** [October 18, 2016, 3:50am UTC](https://community.unix.com/t/modification-to-awk-command/363061/2 "2016-10-18T03:50:29Z")

</div>

Hello SkySmart,

Let's say we have following Input\_file(test one).

```nohighlight
cat Input_file
<?php
  test1
  test2
  phpinfo();
  hlight_file( __FILE__ );
?>
 

```

Then following code may help in same.

```nohighlight
awk '($0 ~ /^\?>$/){print;A=0} ($0 ~ /^<\?php/){A=1} A && ($0 !~ /phpinfo/) && ($0 !~ /hlight/){print}' Input_file

```

Output will be as follows then.

```nohighlight
<?php
  test1
  test2
?>
 

```

Thanks,  
R. Singh

---

<div class="post-metadata">

**Author:** ![SkySmart](https://community.unix.com/letter_avatar/skysmart/32/6_449c533f586939b6e5f246632a46421f.png) [@SkySmart](https://community.unix.com/u/SkySmart)\
**Post date:** [October 18, 2016, 6:48am UTC](https://community.unix.com/t/modification-to-awk-command/363061/3 "2016-10-18T06:48:46Z")

</div>

> [@ravindersingh13](#):
>
> Hello SkySmart,
> 
> Let's say we have following Input\_file(test one).
> 
> ```plaintext
> cat Input_file
> <?php
> test1
> test2
> phpinfo();
> hlight_file( __FILE__ );
> ?>
>  
> 
> ```
> 
> Then following code may help in same.
> 
> ```plaintext
> awk '($0 ~ /^\?>$/){print;A=0} ($0 ~ /^<\?php/){A=1} A && ($0 !~ /phpinfo/) && ($0 !~ /hlight/){print}' Input_file
> 
> ```
> 
> Output will be as follows then.
> 
> ```plaintext
> <?php
> test1
> test2
> ?>
>  
> 
> ```
> 
> Thanks,  
> R. Singh

if the awk code detects the patterns are present, i want it to avoid printing anything at all. thats what i was unable to do with my original code.

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [October 18, 2016, 7:10am UTC](https://community.unix.com/t/modification-to-awk-command/363061/4 "2016-10-18T07:10:58Z")

</div>

Should your system allow for multibyte input and output separators, try

```nohighlight
awk '!(/phpinfo/ && /hlight/)' RS="\?>\n" ORS="?>\n" file

```

---

<div class="post-metadata">

**Author:** ![greet\_sed](https://community.unix.com/letter_avatar/greet_sed/32/6_449c533f586939b6e5f246632a46421f.png) [@greet\_sed](https://community.unix.com/u/greet_sed)\
**Post date:** [October 18, 2016, 9:27am UTC](https://community.unix.com/t/modification-to-awk-command/363061/5 "2016-10-18T09:27:15Z")

</div>

RudiC solution is great. :b:

In sed :

```nohighlight
sed -re '/^<\?php/,/\?>/{N;N;N;/.*phpinfo.*\n.*hlight_file.*\n?>/d}' file

```

```nohighlight
cat file
#!/usr/bin/php
<?php
  phpinfo();
  hlight_file( __FILE__ );
?>
<?php
how
  hlight_file( __FILE__ );
?>
<?php
  phpinfo();
  hlight_file( __FILE__ );
?>
<?php
target
src
?>

```

Gives output:

```nohighlight
#!/usr/bin/php
<?php
how
  hlight_file( __FILE__ );
?>
<?php
target
src
?>

```

---

<div class="post-metadata">

**Author:** ![rdrtx1](https://community.unix.com/user_avatar/community.unix.com/rdrtx1/32/2721_2.png) [@rdrtx1](https://community.unix.com/u/rdrtx1)\
**Post date:** [October 18, 2016, 9:30am UTC](https://community.unix.com/t/modification-to-awk-command/363061/6 "2016-10-18T09:30:26Z")

</div>

```nohighlight
awk '
/<[?]php/ {b=1; php=""; php=php $0 RS; next}
! b ; b {php=php $0 RS}
/[?]>/ {if (php ~ /phpinfo/ && php ~ /hlight/) {php=""} else {printf php}; php=""; b=0}
' infile

```

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [October 18, 2016, 9:59am UTC](https://community.unix.com/t/modification-to-awk-command/363061/7 "2016-10-18T09:59:15Z")

</div>

> [@greet\_sed](#):
>
> .  
> .  
> .  
> In sed :
> 
> ```plaintext
> sed -re '/^<\?php/,/\?>/{N;N;N;/.*phpinfo.*\n.*hlight_file.*\n?>/d}' file
> 
> ```
> 
> .  
> .  
> .

This works well on the sample given. But, the specification doesn't say that

- the two patterns follow in the next TWO consecutive lines
- the order of the patterns is given

Should the be more lines to the records, and should the patterns be reversed, try

```plaintext
sed -re '/^<\?php/ {:L; N; /\?>/!bL; /(phpinfo.*hlight)|(hlight.*phpinfo)/d}' file

```

It reads the entire record no matter how long it is, and then deletes it should both patterns show up in no matter what sequence.

---

<div class="post-metadata">

**Author:** ![SkySmart](https://community.unix.com/letter_avatar/skysmart/32/6_449c533f586939b6e5f246632a46421f.png) [@SkySmart](https://community.unix.com/u/SkySmart)\
**Post date:** [October 18, 2016, 12:23pm UTC](https://community.unix.com/t/modification-to-awk-command/363061/8 "2016-10-18T12:23:16Z")

</div>

> [@rudic](#):
>
> Should your system allow for multibyte input and output separators, try
> 
> ```plaintext
> awk '!(/phpinfo/ && /hlight/)' RS="\?>\n" ORS="?>\n" file
> 
> ```

i would like to do this in awk. but when i run this command, i get this:

```plaintext
awk: warning: escape sequence `\?' treated as plain `?'

```

i intend to use this on Linux Ubuntu, RedHat and AIX systems, so it needs to be portable.

an example of what im trying to do can be summed up this way:

```plaintext
PHPFILE='#!/usr/bin/php
<?php 
  phpinfo();
  hlight_file( __FILE__ );
?>'

CHECK=$(printf "%s\n" "${PHPFILE}" | egrep "phpinfo|hlight_file")

if [-z "${CHECK}"] ; then
  printf "%s\n" "${PHPFILE}"
fi

```

as you can see here, i can accomplish what I want with the above. but, i want it to be shortened through awk so im not making any unnecessary external calls to system tools.

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [October 18, 2016, 12:58pm UTC](https://community.unix.com/t/modification-to-awk-command/363061/9 "2016-10-18T12:58:08Z")

</div>

Try `[?]>\n` .

---

<div class="post-metadata">

**Author:** ![SkySmart](https://community.unix.com/letter_avatar/skysmart/32/6_449c533f586939b6e5f246632a46421f.png) [@SkySmart](https://community.unix.com/u/SkySmart)\
**Post date:** [October 18, 2016, 5:05pm UTC](https://community.unix.com/t/modification-to-awk-command/363061/10 "2016-10-18T17:05:56Z")

</div>

> [@rudic](#):
>
> Try `[?]>\n` .

this fixed it. thank you!

---

<div class="post-metadata">

**Author:** ![looney](https://community.unix.com/letter_avatar/looney/32/6_449c533f586939b6e5f246632a46421f.png) [@looney](https://community.unix.com/u/looney)\
**Post date:** [October 18, 2016, 6:15pm UTC](https://community.unix.com/t/modification-to-awk-command/363061/11 "2016-10-18T18:15:54Z")

</div>

Hi

```nohighlight
sed -re '/^<\?php/,/\?>/{N;N;N;/.*phpinfo.*\n.*hlight_file.*\n?>/d}' file

```

I don't know this type of sed, never studied, Can you please provide any tutorial/link to learn on this.
