Find pattern in a file and erase it


 
Thread Tools Search this Thread
Top Forums Programming Find pattern in a file and erase it
# 1  
Old 06-24-2008
Find pattern in a file and erase it

Hello,

I'd like to find pattern in a file and erase line containing that pattern and 2 lines below that pattern. I know how to find pattern with grep -r pattern and erase line containing that pattern, but can't figure out the rest, how to erase 2 lines below that pattern line. Any suggestions?

Thank you in advance
# 2  
Old 06-24-2008
one way:
Code:
 awk '{if ($0 ~/pattern/) {getline; getline; continue}
            else print }'  old.txt > new.txt

# 3  
Old 06-24-2008
it would be awesome if it worked I get some kind of error "awk: (FILENAME=old.txt FNR=126) fatal: `continue' outside a loop is not allowed" any help ? thank you
# 4  
Old 06-25-2008
My bad - you want 'next', not continue.
# 5  
Old 06-25-2008
its ok jim I found solution elsewhere but thank you anyways Smilie
Code:
sed -i.bak '/pattern/{N;N;d;}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

9 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

How to find and rename of particular pattern in file.?

Hi Guys, I have folder called /input/temp. Inside the folder I have lot of files. I need to find the file of pattern Article_????_test_?????????.txt and replace by format below. Article_????_?????????.txt Below is the one which I tried but it doesn't works can you please help us.... (4 Replies)
Discussion started by: Vinoth Kumar G
4 Replies

3. Shell Programming and Scripting

How to find a file with a specific pattern for current sysdate & upon find email the details?

I need assistance with following requirement, I am new to Unix. I want to do the following task but stuck with file creation date(sysdate) Following is the requirement I need to create a script that will read the abc/xyz/klm folder and look for *.err files for that day’s date and then send an... (4 Replies)
Discussion started by: PreetArul
4 Replies

4. Shell Programming and Scripting

Find file of particular pattern

Hi All, I have a file PSU_ 20130805_201308041234522 i want to search this file where variable day=20130805 and curday=20130804 after currday date some numbers will be added.how to search this file by using day and curday. Thanks in advance. (3 Replies)
Discussion started by: pracheth
3 Replies

5. Shell Programming and Scripting

how to find a pattern from an external file in a directory containing multiple file recursively

Hi, Need your help in this. I have an input file that has multiple enrollment_number, somewhat like 1234567 8901234 9856321 6732187 7623465 Now i have to search and delete these enrollment_number recursively from all the files that are within multiple sub-directories of a... (10 Replies)
Discussion started by: mukulverma2408
10 Replies

6. Shell Programming and Scripting

To find a pattern in file

Hi, I would like to find a pattern in a file as follows: I would like to find "or" "OR" "and" "AND" between two numeric values. I have tried this: grep '**or*' But does not work. Appreciate help on this. (4 Replies)
Discussion started by: pinnacle
4 Replies

7. Shell Programming and Scripting

Find a string in textfile, erase $num lines after that string

I have a textfile containing text similar to the following pattern: STRING1 UNIQUE_STRING1 STRING2 STRING3 STRING4 STRING5 STRING1 UNIQUE_STRING2 STRING2 STRING3 STRING4 STRING5 STRING1 UNIQUE_STRING3 STRING2 STRING3 (6 Replies)
Discussion started by: ilcsfe
6 Replies

8. Shell Programming and Scripting

How to find this pattern in a file

hi all i have a file in my box, which is so huge and full file is in a single line. In this file i have to look for a pattern "ABC01234567" In this above mentioned pattern ABC is fixed and number might change . it will a eight digit random number Thanks so much for all you help ... (1 Reply)
Discussion started by: Prateek007
1 Replies

9. UNIX for Dummies Questions & Answers

erase file

I have a file that always generated in the system eg. /tmp/log.txt , it is generated by the application program , but this file should not be present in the system otherwise there are some program problem , I want to erase this file once the program has generate it , as I know , it can link to... (2 Replies)
Discussion started by: ust
2 Replies
Login or Register to Ask a Question