Deleting lines above the patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Deleting lines above the patterns
# 1  
Old 09-24-2012
CPU & Memory Deleting lines above the patterns

I want to delete 1 line above the paatern and 3 line below the pattern and the pattern line itself, on the whole 5 lines. If there are three patterns what to do and the final text file to be captured in a new file.
# 2  
Old 09-24-2012
Quote:
Originally Posted by razen
I want to delete 1 line above the paatern and 3 line below the pattern and the pattern line itself, on the whole 5 lines. If there are three patterns what to do and the final text file to be captured in a new file.
Please provide input details so somebody can help you out...

try this..(replace pattern with your pattern)

Code:
awk '{if ($0 !~ /pattern/){s=$0;getline;if($0 ~ /pattern/){getline;getline;getline}else{print s"\n"$0}}}' file

# 3  
Old 09-24-2012
fkfkdlfjkdlf
fdfdfdf
12345
lkkklkhj
eyoieue

fjdlkfjdlkjflk
876543
ieiereirei
fkfjdklfjd
fdfdkjdf

euieuie
9807654
fkdfjdkfj
fkdfjkf
uiouoiuiu

fdjkdfjdlkjkld
fdkfjdklfjdlk
fdjfkldjlkf
fdjkfkl
fdkfdkfkd

If a txt file contains above lines, in which I want to delte all the number pattern lines
(aboe and below on the whole 5 lines) and the ultimate file should contain the last unpatterned five lines
# 4  
Old 09-24-2012
Try this..(it removes blank lines)

Code:
awk '{if ($0 !~ /^[0-9][0-9][0-9]/){if(s){print s;s=$0}else{s=$0}}else{getline;getline;getline}}END{if($0 ~ s){print s}}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies

2. UNIX for Beginners Questions & Answers

Delete multiple lines between blank lines containing two patterns

Hi all, I'm looking for a way (sed or awk) to delete multiple lines between blank lines containing two patterns ex: user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 16... (3 Replies)
Discussion started by: ce9888
3 Replies

3. Shell Programming and Scripting

Deleting the lines exist between the Mattched Patterns

Hi, I have a file with the content: for name in \ sree\ rama\ laila\ srihari\ vicky\ john do echo $name done I need to remove all the name lines that exist between for (first line) and do line so that i can replace with new names. Output file should look like: (3 Replies)
Discussion started by: raosr020
3 Replies

4. Shell Programming and Scripting

Deleting lines above the patterns

I want to delete 1 line above the paatern and 3 lines below the pattern and the pattern line itself, on the whole 5 lines. If there are three patterns to be deleted what to do and the final text file to be captured in a new file. (1 Reply)
Discussion started by: razen
1 Replies

5. Shell Programming and Scripting

deleting lines between patterns using sed or awk

hi, Here is excerpt from my xml file <!-- The custom module to do the authentication for LDAP --> </login-module> <login-module code="com.nlayers.seneca.security.LdapLogin" flag="sufficient"> <module-option... (1 Reply)
Discussion started by: sunrexstar
1 Replies

6. Shell Programming and Scripting

How can I get the lines between two patterns?

hi, I have the following file hello world this is to say bye to everyone so bye I want to get the lines from hello to the first bye inclusive into another file? how can I do this (11 Replies)
Discussion started by: JamesByars
11 Replies

7. Shell Programming and Scripting

Searching patterns in 1 file and deleting all lines with those patterns in 2nd file

Hi Gurus, I have a file say for ex. file1 which has 3500 lines in it which are different account numbers and another file (file2) which has 230000 lines in it. I want to read all the lines in file1 and delete all those lines from file2 which has that same pattern as in file1. I am not quite... (4 Replies)
Discussion started by: toms
4 Replies

8. Shell Programming and Scripting

Script for searching a pattern in 5 files and deleting the patterns given

Hi All, I have written the below script that searches for the pattern in a file and delete them if present. please can some one have a look and suggest the changes in the script. #!bin/sh # The pattern that user want to add to the files echo "Enter the pattern of the redirect" read... (4 Replies)
Discussion started by: Shazin
4 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

How to get lines in between Patterns?

Hi, I need to create a script that does the following: 1. Read the file for the occurrences of "EXECUTE" and "END" strings. There will be several occurrences of EXECUTE and END strings on the file. 2. The resulting lines in #1, needs to be searched for the word... (11 Replies)
Discussion started by: racbern
11 Replies
Login or Register to Ask a Question