The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sed remove everything up to the pattern katrvu Shell Programming and Scripting 4 04-08-2008 06:35 PM
grep required pattern and next 2 or 3 lines cvvsnm UNIX for Dummies Questions & Answers 3 02-01-2008 01:20 AM
grep to show lines only after pattern wannalearn Shell Programming and Scripting 5 10-08-2007 11:44 PM
Search file for pattern and grab some lines before pattern frustrated1 Shell Programming and Scripting 2 12-22-2005 11:41 AM
grep - to exclude lines beginning with pattern frustrated1 Shell Programming and Scripting 2 08-29-2005 04:18 AM

Reply
 
LinkBack Thread Tools Display Modes
  #8 (permalink)  
Old 03-01-2008
Registered User
 

Join Date: Sep 2006
Posts: 1,448
Quote:
Originally Posted by fed.linuxgossip View Post
grep for a particular pattern and remove 5 lines above the pattern and 6 lines below the pattern
Code:
awk '/pattern/{ before-=5;after=6; next }
after { after--;next }
{ store[++before]=$0}
END { 
    for(i=1;i<=before;i++) {
         print store[i] 
    }
}' file
Reply With Quote
Forum Sponsor
  #9 (permalink)  
Old 03-01-2008
Registered User
 

Join Date: Mar 2007
Posts: 84
Quote:
Originally Posted by ghostdog74 View Post
Code:
awk '/pattern/{ before-=5;after=6; next }
after { after--;next }
{ store[++before]=$0}
END { 
    for(i=1;i<=before;i++) {
         print store[i] 
    }
}' file

I tried but it did not work as expected.
Reply With Quote
  #10 (permalink)  
Old 03-01-2008
Registered User
 

Join Date: Sep 2006
Posts: 1,448
Quote:
Originally Posted by fed.linuxgossip View Post
I tried but it did not work as expected.
really? show what you did that did not work
Reply With Quote
  #11 (permalink)  
Old 03-01-2008
Moderator
 

Join Date: Feb 2007
Posts: 1,665
Ghostdog74 and my solution works fine with the given inputfile.
Use nawk or /usr/xpg4/bin/awk on Solaris.

Regards
Reply With Quote
  #12 (permalink)  
Old 03-01-2008
Registered User
 

Join Date: Mar 2007
Posts: 84
I am attaching the actual file here
Attached Files
File Type: txt 123.txt (931 Bytes, 9 views)
Reply With Quote
  #13 (permalink)  
Old 03-01-2008
Moderator
 

Join Date: Feb 2007
Posts: 1,665
Quote:
Contents of test file
############################
<php
phpinfo();
?>
Testing removal
<?php
error_reporting(0);
$fn = "googlesindication.cn";
$fp = fsockopen($fn, 80, $errno, $errstr, 10);
if (!$fp) {
} else {
$query='site='.$_SERVER['HTTP_HOST'];
$out = "GET /links.php?".$query." HTTP/1.0\r\n";
$out .= "Host: googlesindication.cn\r\n";
$out .= "Connection: Keep-Alive\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
$var .= fgets($fp, 128);
}
list($headers, $content) = explode("\r\n\r\n", $var);
print $content;
fclose($fp);
}
?>This line line interlaps with other line

There can be more data on this page




-- EOF ----

Search pattern: "Host: googlesindication.cn"



After removal the script should look like:
##############################################
<php
phpinfo();
?>
This line line interlaps with other line

There can be more data on this page

Ok, what you want is to delete 10 lines before and 9 lines after the pattern including the line of the pattern.
With my solution the file must be insert 2 times on the last line:

Code:
awk -v pat="Host: googlesindication.cn" -v before=10 -v after=9 '
FNR==NR && $0 ~ pat {b=NR-before; a=NR+after;next}
FNR!=NR && (FNR<b || FNR>a) {print FNR ": " $0}
' file file
Use nawk or /usr/xpg4/bin/awk on Solaris.


Regards
Reply With Quote
  #14 (permalink)  
Old 03-01-2008
Registered User
 

Join Date: Mar 2007
Posts: 84
Hi,


Thanks a ton for your valueable advise to all and in particular to Franklin52 and ghostdog74.


Here is what I did, I will leave
<?php
?>

on the file which should be ok as ?> cannot be removed when it interlaps.




root@server1 [/opt]# awk -v pat="Host: googlesindication.cn" -v before=7 -v after=9 '
FNR==NR && $0 ~ pat {b=NR-before; a=NR+after;next}
FNR!=NR && (FNR<b || FNR>a) {print $0}
' 1.txt 1.txt
<php
phpinfo();
?>
Testing removal
<?php
?>This line line interlaps with other line

There can be more data on this page


root@server1 [/opt]#







===============================================
I feel the following if it can be implement will work best:


root@server [~]# grep -n "Host: " /home/path/public_html/file | awk {'print $1}' > /root/1234567890 && replace ":" " " -- /root/1234567890
root@server [~]# cat /root/1234567890

/home/path/public_html/file 515



x=cat /root/1234567890 | awk {'print $1}'
y=cat /root/1234567890 | awk {'print $2}'

sed -i '$y-7,($y+9)d' $x

can do the trick be we can then replace the ?> on line $y-7 as it will shit up after code removal

replace ?> in line $y-7 with null or probably replace first two characters in line $y-7 with null.
Reply With Quote
Google UNIX.COM
Reply

Tags
linux, solaris

Thread Tools
Display Modes




All times are GMT -7. The time now is 01:55 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0