![]() |
|
|
|
|
|||||||
| 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 |
|
|||
|
grep for particular pattern and remove few lines above top and bottom of the pattern
grep for a particular pattern and remove 5 lines above the pattern and 6 lines below the pattern
root@server1 [~]# cat filename Shell Programming and Scripting test1 Shell Programminsada asda dasd asd Shell Programming and Scripting Post New Thread Shell Programming and S sadsa sadcripting Post New Thread Shell Progsdaas dsadsaramming and Scripting Post New Thread Shell Programming and Scripting Post New Thread Shell Programming and Scripting Post New Thread Shell Programming and Scripting Post New Thread Shell Programming and Scripting Post New Thread Shell Programming and Scripting Post New Thread pattern_to_remove Shell Programming and Scripting Post New Thread Shell Programming awetrtg teyy teyer nd Scripting Post New Thread Shell Programming and Scripting Post New Thread Shell Programming and Scripting Post New Thread Shell Programming and rewrwt r t Scripting Post New Thread Shell Programming and Scripting Post New Thread Shell Programmingsadas ade and Scripting Post New Thread Shell Programming and Scripting Post New Thread Shell Programming and Scripting Post New Thread Shell Programminsadsa asdad`g and Scripting Post New Thread Prefix each line of output with the line number within its input file. ####################################################################################### root@server1 [~]# grep -n "pattern_" 12345 12 root@server1 [~]# Now I want that 5 lines before line number 12 and 6 lines after line number 12 be replaced with null or removed(deleted) with a script Please advise Last edited by fed.linuxgossip; 02-29-2008 at 08:46 AM. Reason: spell |
| Forum Sponsor | ||
|
|
|
|||
|
It could be done with nawk.
2 ways: getting a line numbers and skip line between; or keep all the time last 6 (in your case) lines, and print 7-th back line. Here is 1-st way solution: Code:
# prepare file for testing:
n=0; fl=for_removing_lines.txt; rmv_lbl="point of removing"; rm $fl;
while [ $n -le 20 ];do
(( n++ )); echo "line $n">>$fl;
if [ $n -eq 10 ]; then
echo $rmv_lbl>>$fl;
fi;
done;
# geting line numbers to statr and END remuving
pnt_ln=`nawk -v srch="$rmv_lbl" '{if($0~srch) print NR; }' $fl`;
((rmv_st=pnt_ln-6));
((rmv_end=pnt_ln+6)); echo $pnt_ln, $rmv_st, $rmv_end
# printing file without 6 lines before and after label line
nawk -v st=$rmv_st -v end=$rmv_end '{if( (NR <= st)||(NR >= end) ) print $0; }' $fl
|
|
|||
|
Quote:
Also I can not understand what and how did you try to do. |
|
|||
|
Hello,
root@server [~]# grep -n "Host: " /home/path/public_html/* | awk {'print $1}' > /root/1234567890 && replace ":" " " -- /root/1234567890 root@server [~]# cat /root/1234567890 /home/path/public_html/file12.htm 515 /home/path/public_html/file19.htm 1662 /home/path/public_html/file26.htm 2245 /home/path/public_html/file5.htm 509 /home/path/public_html/file15.htm 2178 /home/path/public_html/file1.htm 1837 /home/path/public_html/file22.htm 1746 /home/path/public_html/file29.htm 507 I have now the line number in which the pattern is present, and its present only once is any file. The pattern in this case is "Host: " can you advise a script that will do the following: x=cat /root/1234567890 | awk {'print $1}' y=cat /root/1234567890 | awk {'print $2}' sed -i '$y-7,($y+9)d' $x Thanks |
|||
| Google UNIX.COM |