grep for a particular pattern and remove few lines above top and bottom of the patter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep for a particular pattern and remove few lines above top and bottom of the patter
# 8  
Old 03-01-2008
Quote:
Originally Posted by fed.linuxgossip
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

# 9  
Old 03-01-2008
Quote:
Originally Posted by ghostdog74
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.
# 10  
Old 03-01-2008
Quote:
Originally Posted by fed.linuxgossip
I tried but it did not work as expected.
really? show what you did that did not work
# 11  
Old 03-01-2008
Ghostdog74 and my solution works fine with the given inputfile.
Use nawk or /usr/xpg4/bin/awk on Solaris.

Regards
# 12  
Old 03-01-2008
I am attaching the actual file here
# 13  
Old 03-01-2008
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
# 14  
Old 03-01-2008
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing top few and bottom few rows in a file

Hi, I have a requirement where I need to delete given number of top and bottom rows in a flat file which has new line as its delimiter. For ex: if top_rows=2 & bottom_rows=1 Then in a given file which looks like: New York DC LA London Tokyo Prague Paris Bombay Sydney... (7 Replies)
Discussion started by: calredd
7 Replies

2. Shell Programming and Scripting

Remove top and bottom for each column

Dear All I was wondering if someone could help me in resolving an issue. I have a file like this: column1 column2 2 4 3 5 8 9 0 12 0 0 0 0 9 0 87 0 1 0 1 0 1 0 4 0 (2 Replies)
Discussion started by: giuliangiuseppe
2 Replies

3. Shell Programming and Scripting

How to print from bottom to top?

Hi, i have a file which contains PID and wanted to execute kill command. but the thing is, when killing PID's needs to kill PID from bottom to top. Please help INPUT 21414 sh -c extract.ksh ASA 21416 /bin/ksh extract.ksh ASA 21428 /usr/bin/perl -w /var/tmp/tempperl.21416 ASA... (4 Replies)
Discussion started by: reignangel2003
4 Replies

4. Ubuntu

Title bar top and bottom

Hello forum, Seems that only I have alot of questions regarding Ubuntu :D In Ubuntu 12.04 LTS the gnome I have been using gdm and lightdm. In lightdm the top and side bars are aka "unity" and can be removed using apt-get remove unity I need to do the same for menu bars gdm. I do not... (0 Replies)
Discussion started by: br1an
0 Replies

5. Shell Programming and Scripting

Print n lines from top and n lines from bottom of all files with .log extenstion

Oracle Linux 6.4 In a directory I have more than 300 files with the extension .log I want the first 5 and last 5 lines of these .log files to be printed on screen with each file's name. Expected output : Printing first 5 and last 5 lines of FX_WT_Feb8_2014.log !! Authentication... (7 Replies)
Discussion started by: kraljic
7 Replies

6. Shell Programming and Scripting

Remove x lines form top and y lines form bottom using AWK?

How to remove x lines form top and y lines form bottom. This works, but like awk only cat file | head -n-y | awk 'NR>(x-1)' so remove last 3 lines and 5 firstcat file | head -n-3 | awk 'NR>4' (5 Replies)
Discussion started by: Jotne
5 Replies

7. Shell Programming and Scripting

Search for string and print top and bottom line

Hi Folks I need a one liner to parse through a log and if the string is found print the line above, the line with the string and the line below. example: The ball is green and blue Billy through the ball higer. Jane got hurt with the ball. So if I search for Billy I would need the 3... (1 Reply)
Discussion started by: bombcan
1 Replies

8. UNIX for Dummies Questions & Answers

Grep for a pattern based on another patter

hi, I have looked at many grep threads and am unable to find something like this: please help. I have a file which is generated from a report generator and i am trying to load a whole lot of specific data into a table for the users. One field is causing me problems.All the rest i can manage.... (7 Replies)
Discussion started by: rock1
7 Replies

9. Shell Programming and Scripting

How to search for a pattern from bottom to top.

Hi, I have a file, which is having a pattern "SEARCH" somewhere towards end of the file, if i am giving " grep -i "SEARCH" $File" , it is taking too much time as file is very big. So i want to search for the pattern from the back side of the file, how can we search for a pattern in bottom... (5 Replies)
Discussion started by: Prat007
5 Replies
Login or Register to Ask a Question