![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Rules & FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
Quote:
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
|
| Forum Sponsor | ||
|
|
|
|||
|
Quote:
I tried but it did not work as expected. |
|
|||
|
Quote:
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
Regards |
|
|||
|
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. |
|||
| Google UNIX.COM |