Modification to awk command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Modification to awk command
# 1  
Old 10-18-2016
Modification to awk command

i have a php file that has this:

php.code

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:

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

# 2  
Old 10-18-2016
Hello SkySmart,

Let's say we have following Input_file(test one).
Code:
cat Input_file
<?php
  test1
  test2
  phpinfo();
  hlight_file(__FILE__);
?>

Then following code may help in same.
Code:
awk '($0 ~ /^\?>$/){print;A=0} ($0 ~ /^<\?php/){A=1} A && ($0 !~ /phpinfo/) && ($0 !~ /hlight/){print}'   Input_file

Output will be as follows then.
Code:
<?php
  test1
  test2
?>

Thanks,
R. Singh

Last edited by RavinderSingh13; 10-18-2016 at 06:48 AM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 10-18-2016
Quote:
Originally Posted by RavinderSingh13
Hello SkySmart,

Let's say we have following Input_file(test one).
Code:
cat Input_file
<?php
  test1
  test2
  phpinfo();
  hlight_file(__FILE__);
?>

Then following code may help in same.
Code:
awk '($0 ~ /^\?>$/){print;A=0} ($0 ~ /^<\?php/){A=1} A && ($0 !~ /phpinfo/) && ($0 !~ /hlight/){print}'   Input_file

Output will be as follows then.
Code:
<?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.

Last edited by SkySmart; 10-18-2016 at 08:02 AM..
# 4  
Old 10-18-2016
Should your system allow for multibyte input and output separators, try
Code:
awk '!(/phpinfo/ && /hlight/)' RS="\?>\n" ORS="?>\n" file

These 2 Users Gave Thanks to RudiC For This Post:
# 5  
Old 10-18-2016
RudiC solution is great. Smilie

In sed :

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

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

This User Gave Thanks to greet_sed For This Post:
# 6  
Old 10-18-2016
Code:
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

# 7  
Old 10-18-2016
Quote:
Originally Posted by greet_sed
.
.
.
In sed :

Code:
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

Code:
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.

Last edited by RudiC; 10-18-2016 at 11:06 AM..
These 2 Users Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with command modification

Hello folks; I'm using the following command to get the highest number of requests per second in a log file and it works well. grep "2017-02-22" "LogFile.log" | cut -c1-20 | uniq -c | sort -n | tail -n1 Now i would like to also get the smallest requests per second and the amount of time... (5 Replies)
Discussion started by: Katkota
5 Replies

2. Shell Programming and Scripting

awk script modification

can someone help me identify what i'm doing wrong here: awk -F'|' 'BEGIN{c=0} /./ && /./ { if ($3 < 2) { print ; c++ } END { print c":OK" } else if (($3 >= 2) && ($3 < 4)) { print ; c++ } END { print c":WARNING" } else if ($3 >= 4) { print ; c++ } END { print c":CRITICAL" } }'... (4 Replies)
Discussion started by: SkySmart
4 Replies

3. Shell Programming and Scripting

Perl command modification

below is a snippet of code from a larger perl code: my $uname = ( -e '/usr/bin/uname' ) ? '/usr/bin/uname' : '/bin/uname'; my $os = ( `$uname 2>/dev/null` ); when i run this code, it seems to be complaining about the backticks. is there any efficient way i can get rid of the backticks... (3 Replies)
Discussion started by: SkySmart
3 Replies

4. Shell Programming and Scripting

IP Address Modification through awk/sed

Hi, I have to modify the 2nd and 3rd octet of the IP address through awk/sed. For Example: Given IP is : 10.205.22.254, it should be modified as 10.105.100.254 through awk/sed. Kindly help me on this and let me know if you have any questions. Thanks in advances. (2 Replies)
Discussion started by: kumarbka
2 Replies

5. UNIX for Dummies Questions & Answers

awk output modification

Hello, I am using awk command to print some output, but there are some characters that I would like to remove from the output awk '{print $5$6}' the output I get is column5/:column6 I am looking forward to remove the : and to get the output column5/column6 Sorry if this question is... (4 Replies)
Discussion started by: Error404
4 Replies

6. Shell Programming and Scripting

awk modification for lines

so i have this data in a file: jime=1860,yime=1.23243,lime= jime=1859,yime=1.23018,lime= jime=1825,yime=1.15371,lime= jime=1849,yime=1.20769,lime= jime=1841,yime=1.1897,lime= jime=1849,yime=1.20769,lime= i use this code to calculate the percentage difference of the number in column 2... (9 Replies)
Discussion started by: SkySmart
9 Replies

7. Shell Programming and Scripting

awk script modification

I want the below script to omit every chunk of data that contains a specific hostname. here's the scenario. i have a configuration file that contains the configuration of several hosts. a sample of this configuration file is this: define host { address ... (12 Replies)
Discussion started by: SkySmart
12 Replies

8. Shell Programming and Scripting

Awk modification

I need help modifying the code below. DATAFILE is a log file. I have two strings i need to search for in the log file. The two strings are: 1. ERROR 2. com.rolander.promotions.client awk 'BEGIN { while((getline < "'${SFILE}'")>0) S FS="\n"; RS="\n" } (11 Replies)
Discussion started by: SkySmart
11 Replies

9. Shell Programming and Scripting

in line modification in a file using awk

Hi, I have a conf.file with the following values: ef=78 b=40 ca=40 c=45/dev2 when I modify one of the line with the below awk script,it's modifying BUT it's placing the modified line in the last line : input:- Configure b 45/dev4 output:- ef=78 ca=40 ... (2 Replies)
Discussion started by: axes
2 Replies

10. UNIX for Dummies Questions & Answers

command for modification date of a file

Good morning, I would like to find all files of a certain type and display their name as well as their modification date. In order to do this, I would do the following: find ./ -name *.csv | ???????? My question: what to put after the pipe instead of the question marks? Is there a basic... (5 Replies)
Discussion started by: scampsd
5 Replies
Login or Register to Ask a Question