retaining only the second line with a pattern and deleting all others


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting retaining only the second line with a pattern and deleting all others
# 1  
Old 10-28-2011
retaining only the second line with a pattern and deleting all others

Hi,

I have a file:

5 T1AxialPremosaic ok 512 448 23 1 284000-000005-000001.dcm
6 T2_SPACE ok 256 256 176 1 465000-000006-000001.dcm
7 FLAIRmosaic ok 512 432 23 1 748000-000007-000001.dcm
8 T2_SPACE ok 256 256 1 171000-000008-000001.dcm
9 T2_SPACE ok 256 256 1 218000-000009-000001.dcm
32 T1AxialPostmosaic ok 512 448 23 1 836000-000032-000001.dcm
33 MEMPRAGE ok 256 256 704 1 408000-000033-000001.dcm
34 MEMPRAGE ok 256 256 176 1 767000-000034-000001.dcm
35 MEMPRAGE ok 256 256 1 380000-000035-000001.dcm
36 MEMPRAGE ok 256 256 1 178000-000036-000001.dcm

...and I would like to retain only the second line with the pattern MEMPRAGE (preferably using just RAGE and in a case-insensitive manner). I've tried different things with awk:

Code:
awk 'tolower($0) ~ /rage/&&c++>0 {next} 1' inputfile

but for this guy (above), I don't know how to keep only the second occurrence. I've also tried various plays on this one-liner:

Code:
awk '(substr($0,8,4)!="RAGE")||(NR<1)' inputfile

But this isn't quite right either. The bottom line is that I don't understand awk, sed or grep well enough to get it done quickly. Any help would be greatly appreciated. Thanks.
# 2  
Old 10-28-2011
Try:
Code:
awk 'tolower($0)~/rage/&&++i==2' input

# 3  
Old 10-28-2011
What about something like?

Code:
$ grep MEMPRAGE <sample8.txt | head -2 | tail -1
34 MEMPRAGE ok 256 256 176 1 767000-000034-000001.dcm

# 4  
Old 10-28-2011
This works great, but I actually realize my mistake in writing that I wished to retain everything but the line with the second occurrence of the pattern. I would like to retain everything in the text file without the tag "RAGE", as well as the line with the second occurrence of "RAGE". So for example, this would be my ideal output:


5 T1AxialPremosaic ok 512 448 23 1 284000-000005-000001.dcm
6 T2_SPACE ok 256 256 176 1 465000-000006-000001.dcm
7 FLAIRmosaic ok 512 432 23 1 748000-000007-000001.dcm
8 T2_SPACE ok 256 256 1 171000-000008-000001.dcm
9 T2_SPACE ok 256 256 1 218000-000009-000001.dcm
32 T1AxialPostmosaic ok 512 448 23 1 836000-000032-000001.dcm
34 MEMPRAGE ok 256 256 176 1 767000-000034-000001.dcm

Any ideas on how to keep the rest of the file intact (except for lines with "RAGE")? Apologies for the follow-up here.
# 5  
Old 10-28-2011
Try:
Code:
awk 'tolower($0)~"rage"&&++i==2||tolower($0)!~"rage"' input

# 6  
Old 10-28-2011
You're the best. Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Get an output of lines in pattern 1st line then 10th line then 11th line then 20th line and so on.

Input file: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (6 Replies)
Discussion started by: Sagar Singh
6 Replies

2. Shell Programming and Scripting

awk to combine lines from line with pattern match to a line that ends in a pattern

I am trying to combine lines with these conditions: 1. First line starts with text of "libname VALUE db2 datasrc" where VALUE can be any text. 2. If condition1 is met then continue to combine lines through a line that ends with a semicolon. 3. Ignore case when matching patterns and remove any... (5 Replies)
Discussion started by: Wes Kem
5 Replies

3. UNIX for Dummies Questions & Answers

Deleting a pattern in UNIX without deleting the entire line

Hi I have a file: r58778.3|SOURCES={KEY=f665931a...,fw,221-705}|ERRORS={16_1:T,30_1:T,56_1:C,57_1:T,59_1:A,101_1:A,115:-,158_1:C,186_1:A,204:-,271_1:T,305:-,350_1:C,368_1:G,442_1:C,472_1:G,477_1:A}|SOURCE_1="Contig_1092402550638"(f665931a359e36cea0976db191ff60ff09cc816e) I want to retain... (15 Replies)
Discussion started by: Alyaa
15 Replies

4. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

5. Shell Programming and Scripting

Deleting pattern without removing line

I am trying to delete a pattern without removing line. I searched a lot in this forum and using those I could come up with sed command but it seems that command does not work. Here's how my file looks like: 1 ./63990 7 1171 ./63990 2 2425 ./63990 9 2539 ./63990 1 3125 ./63990 1 10141... (7 Replies)
Discussion started by: shoaibjameel123
7 Replies

6. Shell Programming and Scripting

Searching a pattern in file and deleting th ewhole line containing the pattern

Hi All, Please can someone assist in the script I have made that searches a pattern in a file and delete the whole line containing the pattern. #!bin/sh # The pattern that user want to add to the files echo "Enter the pattern of the redirect" read value # check if the user has... (1 Reply)
Discussion started by: Shazin
1 Replies

7. Shell Programming and Scripting

deleting a pattern from a file

say i have a file with the following contents 0x20 0x20 0xc23886 > 0xc12354 > 0xc567555555 i want to delete "> " pattern and keep the rest of the file (6 Replies)
Discussion started by: lassimanji
6 Replies

8. Shell Programming and Scripting

find pattern, delete line with pattern and line above and line below

I have a file that will sometimes contain a pattern. The pattern is this: FRM CHK 0000 I want to find any lines with this pattern, delete those lines, and also delete the line above and the line below. (4 Replies)
Discussion started by: nickg
4 Replies

9. UNIX for Dummies Questions & Answers

find pattern delete line with pattern and line above and line below

I have a file that will sometimes contain a pattern. The pattern is this: W/D FRM CHK 00 I want to find any lines with this pattern, delete those lines, and also delete the line above and the line below. (1 Reply)
Discussion started by: nickg
1 Replies

10. Shell Programming and Scripting

prom in deleting a pattern

hi guys, i have a directory.inside that so many directories and files are there.i want to search the complete directory for a pattern in all the .txt files.if i will find that pattern then i want to delete that pattern from that file. please help me out. i want it urgent (18 Replies)
Discussion started by: suvendu4urs
18 Replies
Login or Register to Ask a Question