sed: deleting 5 lines after a specified pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed: deleting 5 lines after a specified pattern
# 1  
Old 08-13-2009
sed: deleting 5 lines after a specified pattern

As an example (just an example, this could apply to any block of text) say I have this:

Code:
architecture x86_64
cputype CPU_TYPE_X86_64
cpusubtype CPU_SUBTYPE_X86_64_ALL
offset 4096
size 2972420
align 2^12 (4096)
architecture ppc64
cputype CPU_TYPE_POWERPC64
cpusubtype CPU_SUBTYPE_POWERPC_ALL
offset 2977792
size 2702672
align 2^12 (4096)

I want to delete the line "architecture x86_64, as well the next 5 lines that come right after it. How would I go about doing that? Thanks
# 2  
Old 08-13-2009
Code:
nawk '/architecture x86_64/ {c=6} c && c-- {next}1' myFile

# 3  
Old 08-13-2009
Code:
sed '/archictecture/,5d;p' filename

example depends on your sed...

edit correction:
Code:
sed -n  '/archictecture x86_64/,5d;p' filename


Last edited by jim mcnamara; 08-14-2009 at 09:13 AM..
# 4  
Old 08-14-2009
Hi.

Some versions of grep or grep-like utilities can do this. For example, the very versatile cgrep can. Here is a sample of first extracting the string plus the trailing window, then the inversion of that operation, which effectively deletes it, both operations writing to STDOUT:
Code:
#!/usr/bin/env bash

# @(#) s2	Demonstrate effectively deleting lines with cgrep.
# http://www.bell-labs.com/project/wwexptools/cgrep/

echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1)
set -o nounset
echo

FILE=${1-data1}

echo " Data file $FILE (numbered for this display):"
cat -n $FILE
wc -l $FILE

rm -f t1
echo
echo " Results of matching + copy following:"
cgrep -D +5 "architecture x86_64" $FILE > t1
cat t1
echo " Lines extracted: $(wc -l <t1)"

rm -f t1
echo
echo " Results of matching + copy following with inversion flag:"
cgrep -D -V +5 "architecture x86_64" $FILE > t1
cat t1
echo " Lines extracted: $(wc -l <t1)"

exit 0

producing:
Code:
% ./s2

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
GNU bash 3.2.39

 Data file data1 (numbered for this display):
     1	architecture x86_64
     2	cputype CPU_TYPE_X86_64
     3	cpusubtype CPU_SUBTYPE_X86_64_ALL
     4	offset 4096
     5	size 2972420
     6	align 2^12 (4096)
     7	architecture ppc64
     8	cputype CPU_TYPE_POWERPC64
     9	cpusubtype CPU_SUBTYPE_POWERPC_ALL
    10	offset 2977792
    11	size 2702672
    12	align 2^12 (4096)
12 data1

 Results of matching + copy following:
architecture x86_64
cputype CPU_TYPE_X86_64
cpusubtype CPU_SUBTYPE_X86_64_ALL
offset 4096
size 2972420
align 2^12 (4096)
 Lines extracted: 6

 Results of matching + copy following with inversion flag:
architecture ppc64
cputype CPU_TYPE_POWERPC64
cpusubtype CPU_SUBTYPE_POWERPC_ALL
offset 2977792
size 2702672
align 2^12 (4096)
 Lines extracted: 6

So cgrep is not very diffferent from sed when cgrep is used this way. Where it might be superior to sed is in the selection of context window limits: regular expressions and line counts (the latter as shown above) can be used to mark easily the leading and trailing boundaries of a window.

You may need to download and compile cgrep, see the URL in the code above. I have done so on 32-bit and 64-bit GNU/Linux systems.

Best wishes ... cheers, drl

Last edited by vgersh99; 08-14-2009 at 12:16 PM.. Reason: fixed code tag
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

2. Shell Programming and Scripting

Deleting lines on matching certain pattern

hi I have a large xml file from which i have taken few lines . In this file I have to find for the string </invoices> and check if the 3 rd line after this string does not begin with <portCode> ,then i have to delete the string </invoices> and the next line having the string </shippingBill>... (13 Replies)
Discussion started by: sunnyboy
13 Replies

3. Shell Programming and Scripting

Vi editor deleting lines with specific pattern

Hi, I need to delete all lines in the file using vi editor which start with word aternqaco. Please assist. aternqaco.__oracle_base='/amdbqa01/app/oracle'#ORACLE_BASE set from environment aternqa.__oracle_base='/amdbqa01/app/oracle'#ORACLE_BASE set from environment... (3 Replies)
Discussion started by: Vishal_dba
3 Replies

4. Shell Programming and Scripting

Help with a deleting lines based on a pattern

I have a header-detail file that goes like this: SHP00288820131021110921 ORDER0156605920131021110921INMMMMFN DETAIL0004 4C2Z 10769 AAFC 0000009600000094 4C2Z 10769 AAFC 0000672107 OIL DETAIL0002 ER3Z 14300 E 0000001300000012 ER3Z 14300 E 0000672107 OIL... (3 Replies)
Discussion started by: rbaggio666
3 Replies

5. Shell Programming and Scripting

Deleting lines from a stream after matching a pattern

Hi, I have a requirement to to an ldapsearch and remove the shadow attributes in the output file. What I do is ldapsearch() | operation to remove shadow > FILE The ldapsearch gives output like this(with same line formation): objectClass: FSConfig objectClass: extensibleObject fsCAIP:... (10 Replies)
Discussion started by: lorzinian
10 Replies

6. UNIX for Dummies Questions & Answers

[Solved] deleting pattern based lines in sed

HI, My input file contains below data: DFHDR 12345110 1,200 2,-100 1,100 2,123 12345110 1,300 2,200 DFTLR In the above data, the first line and last lines should be remove as well as the lines in which contains 110 as position(6,7,8 position) should also be removed, How we... (0 Replies)
Discussion started by: pandeesh
0 Replies

7. Shell Programming and Scripting

deleting lines in a file that match a pattern without opening it

In Unix, how do I delete lines in a file that match a particular pattern without opening it. File contents are foo line1 misc whatever foo line 2 i want to delete all lines that have the pattern "foo" without opening the file. File should eventually contain misc whatever (1 Reply)
Discussion started by: osbourneric
1 Replies

8. Shell Programming and Scripting

pattern matching over multiple lines and deleting the first

I've got a longish log file with content such as Uplink traffic: Downlink traffic: I want to parse the log file and remove any line that contains the string "Uplink traffic:" at the beginning of the line, but only if the line following it beginnings with the string "Downlink traffic:" (in... (7 Replies)
Discussion started by: Yorkie99
7 Replies

9. Shell Programming and Scripting

Delete lines between two patterns without deleting the second pattern

I want to delete lines like this sed '/FROM_HERE/,/TO_HERE/d' but I would like to *not* delete the second match, i.e. the TO_HERE line. How can I achieve this? Thank you! (1 Reply)
Discussion started by: Ilja
1 Replies

10. Shell Programming and Scripting

deleting lines after pattern using sed

I have seen there are many sed posts but still it is quite difficult to apply other post to my own problem How can I delete all lines in a file from 2 lines after this pattern *End_fine_coreg:_NORMAL to the end of file? Cheers (2 Replies)
Discussion started by: larne
2 Replies
Login or Register to Ask a Question