Today (Saturday) We will make some minor tuning adjustments to MySQL.

You may experience 2 up to 10 seconds "glitch time" when we restart MySQL. We expect to make these adjustments around 1AM Eastern Daylight Saving Time (EDT) US.


Find to delete lines with pattern and even or odd number


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Find to delete lines with pattern and even or odd number

In the below directory I am trying to delete all lines with a .bam extention that have the pattern IonCode_ followed by an even number. I am also trying to delete all lines with a .fastq extention that have the pattern IonCode_ followed by an odd number. I was going to use find but can see all lines result with the code as is. Thank you Smilie.


Code:
IonCode_0281.bam     --- keep
IonCode_0282.bam     --- delete
IonCode_0281.fastq   --- delete
IonCode_0282.fastq   --- keep

Code:
find . -maxdepth 1 -type f -iname "IonCode_[02468]*.bam" #delete    --- remove even
find . -maxdepth 1 -type f -iname "IonCode_[13579]*.fastq" #delete    --- remove odd

desired
Code:
IonCode_0281.bam
IonCode_0282.fastq

# 2  
Code:
find . -maxdepth 1 -type f | awk -F'[._]' '($NF=="bam" && !($(NF-1)%2)) || ($NF=="fastq" && $(NF-1)%2) {print "rm " $0}'

When happy with the results, pipe to "sh": ..... | sh
This User Gave Thanks to vgersh99 For This Post:
# 3  
Select on the last digit
Code:
find . -maxdepth 1 -type f -iname "IonCode_*[02468].bam" -print
find . -maxdepth 1 -type f -iname "IonCode_*[13579].fastq" -print

To really delete, replace -print with -delete.
These 2 Users Gave Thanks to MadeInGermany For This Post:
# 4  
In case you get charged by the find:
Code:
find . -maxdepth 1 -type f \( -iname "IonCode_*[13579].fastq" -o -iname "IonCode_*[02468].bam" \) -delete

or (gnu-ish)
Code:
find . -maxdepth 1 -type f -iregex '.*/IonCode_.*\([02468][.]bam\|[13579][.]fastq\)' -delete

This User Gave Thanks to rdrtx1 For This Post:
# 5  
I made a typo in the filenames, I apologize. The bold section is the even or odd number used. I think the 00-000 is being used and can not seem to have it look before the 00-000. Thank you Smilie.

Code:
IonCode_0281-00-000.bam     --- keep
IonCode_0282-00-000.bam     --- delete
IonCode_0281-00-000.fastq   --- delete
IonCode_0282-00-000.fastq   --- keep


Last edited by cmccabe; 02-19-2019 at 10:02 AM..
# 6  
Code:
find . -maxdepth 1 -type f -iregex '.*/IonCode_.*\([0-9]+[02468]-[0-9]*-[0-9]*[.]bam\|[0-9]+[13579]-[0-9]*-[0-9]*[.]fastq\)' -delete

This User Gave Thanks to rdrtx1 For This Post:
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
awk with sed to combine lines and remove specific odd # pattern from line
cmccabe
In the awk piped to sed below I am trying to format file by removing the odd xxxx_digits and whitespace after, then move the even xxxx_digit to the line above it and add a space between them. There may be multiple lines in file but they are in the same format. The Filename_ID line is the last line...... UNIX for Beginners Questions & Answers
4
UNIX for Beginners Questions & Answers
How to delete all lines before a particular pattern when the pattern is defined in a variable?
Soham
I have a file Line 1 a Line 22 Line 33 Line 1 b Line 22 Line 1 c Line 4 Line 5 I want to delete all lines before last occurrence of a line which contains something which is defined in a variable. Say a variable var contains 'Line 1', then I need the following in the output. ...... Shell Programming and Scripting
21
Shell Programming and Scripting
sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern
Klasform
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>...... Shell Programming and Scripting
9
Shell Programming and Scripting
How to search for pattern in odd lines?
kaaliakahn
Hi friends, I am looking for sed command/script that would search for a given fixed pattern on odd lines and then if it matches, prints the matching pattern and the next line. For example, in the example below, i am looking for pattern 0 and 1011 on odd lines. ########## start of example file...... Shell Programming and Scripting
10
Shell Programming and Scripting
Find pattern a delete previous 5 lines
EjjE
Hi guys, i have the follow problem i need to delete 10 row before the pattern and 1 after and the pattern row itself. file looks like: frect 9.8438 25.8681 10.625 25 . dynprop \ (# \ (call fox_execute(__self))) \ (FOX_VAR_29 \ ...... Shell Programming and Scripting
4
Shell Programming and Scripting

Featured Tech Videos