Modification to awk command

i have a php file that has this:

php.code

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

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

Hello SkySmart,

Let's say we have following Input_file(test one).

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

Then following code may help in same.

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

Output will be as follows then.

<?php
  test1
  test2
?>
 

Thanks,
R. Singh

1 Like

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.

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

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

RudiC solution is great. :b:

In sed :

sed -re '/^<\?php/,/\?>/{N;N;N;/.*phpinfo.*\n.*hlight_file.*\n?>/d}'  file
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:

#!/usr/bin/php
<?php
how
  hlight_file(__FILE__);
?>
<?php
target
src
?>
1 Like
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

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

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.

2 Likes

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

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:

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.

Try [?]>\n .

1 Like

this fixed it. thank you!

Hi

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.