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
# 1  
Old 02-29-2008
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
12Smilieattern_to_remove
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 12:46 PM.. Reason: spell
# 2  
Old 02-29-2008
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  
Old 02-29-2008
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

Regards
# 4  
Old 02-29-2008
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  
Old 02-29-2008
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

Producing:
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

I like this approach for small jobs because the syntax of the range is so understandable -- from the pattern so many lines back, to the pattern plus so many lines forward.

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 07:28 PM..
# 6  
Old 02-29-2008
Quote:
Originally Posted by Franklin52
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

Regards
I can not get it work.
Also I can not understand what and how did you try to do.
# 7  
Old 03-01-2008
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
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