![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum 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 02: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 12:41 PM |
| grep - to exclude lines beginning with pattern | frustrated1 | Shell Programming and Scripting | 2 | 08-29-2005 04:18 AM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 09:46 AM. Reason: spell |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
Have a look for a utility called vmsgrep. I've got the source code if you need it. It does exactly what you are asking to do (pull out a specified number of lines above/below your pattern match).
|
|
#3
|
|||
|
|||
|
A brute force awk solution:
Code:
awk -v pat="pattern_" -v before=5 -v after=6 '
FNR==NR && $0 ~ pat {b=NR-before;a=NR+after;next}
FNR!=NR && (FNR<b || FNR>a) {print FNR ": " $0}
' file file
|
|
#4
|
|||
|
|||
|
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
|
|
#5
|
||||
|
||||
|
Hi.
Using an older tool, the programmable interactive line editor ed or ex: Code:
#!/usr/bin/env sh
# @(#) s3 Demonstrate deleting a range with line editor.
# ____
# /
# | Infrastructure BEGIN
echo
set -o nounset
debug=":"
debug="echo"
## The shebang using "env" line is designed for portability. For
# higher security, use:
#
# #!/bin/sh -
## Use local command version for the commands in this demonstration.
EDITOR=ex
EDITOR=ed
set +o nounset
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version =o $(_eat $0 $1) $EDITOR
set -o nounset
echo
FILE=data1
cp sacred $FILE
echo " Input file $FILE:"
cat $FILE
before=5
after=6
pattern="pattern"
# | Infrastructure END
# \
# ---
echo
echo " Output from editor $EDITOR:"
$EDITOR <<EOF $FILE
/$pattern/-${before},/$pattern/+${after}d
w
q
EOF
echo
echo " Output file $FILE:"
cat $FILE
exit 0
Code:
% ./s3 (Versions displayed with local utility "version") Linux 2.6.11-x1 GNU bash, version 2.05b.0(1)-release (i386-pc-linux-gnu) GNU ed version 0.2 Input file data1: -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 pattern 1 2 3 4 5 6 7 8 9 10 Output from editor ed: 60 25 Output file data1: -10 -9 -8 -7 -6 7 8 9 10 A disadvantage is that this can be slow on large files because the entire file is read. See man pages for details ... cheers, drl ( edit 1: typo ) Last edited by drl; 02-29-2008 at 04:28 PM. |
|
#6
|
|||
|
|||
|
Quote:
Also I can not understand what and how did you try to do. |
|
#7
|
|||
|
|||
|
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 The UNIX and Linux Forums |