justaguy
justaguy

Reputation: 3022

bash to capture specific instance of pattern and exclude others

I am trying to capture and read into $line the line or lines in file that have only del in them (line 2 is an example). Line 3 has del in it but it also has ins and the bash when executed currently captures both. I am not sure how to exclude anything but del and only capture those lines. Thank you :).

file

NM_003924.3:c.765_779dupGGCAGCGGCGGCAGC
NM_003924.3:c.765_779delGGCAGCGGCGGCAGC
NM_003924.3:c.765_779delGGCAGCinsGGCGGCAGC
NM_003924.3:c.765_779insGGCAGCGGCGGCAGC

desired output

NM_003924.3:c.765_779delGGCAGCGGCGGCAGC

bash w/ current output

while read line; do
  if [[ $line =~ del ]] ; then echo $line; fi
done < file

NM_003924.3:c.765_779delGGCAGCGGCGGCAGC
NM_003924.3:c.765_779delGGCAGCinsGGCGGCAGC

Upvotes: 1

Views: 54

Answers (5)

Walter A
Walter A

Reputation: 20032

Split it in 2 steps. You do not need a loop:

grep "del" file | grep -v "ins"

Upvotes: 1

iamauser
iamauser

Reputation: 11489

Here is another answer using PCRE enabled grep. This should work with -P option in GNU grep

$ grep -P 'del\.*(?!.*ins)' inputFile
NM_003924.3:c.765_779delGGCAGCGGCGGCAGC

Upvotes: 1

iamauser
iamauser

Reputation: 11489

Here is a sed solution. It negates the match del followed by ins and prints everything that has a del in it. -n to silent every other output.

$ sed -n -e '/del.*ins/!{/.*del.*/p}' inputFile
NM_003924.3:c.765_779delGGCAGCGGCGGCAGC

Upvotes: 1

pjh
pjh

Reputation: 8209

Try:

while read -r line; do
    [[ $line =~ del && ! $line =~ ins ]] && printf '%s\n' "$line"
done < file

The revised code is also ShellCheck clean and avoids BashPitfall #14.

This solution may fail if the last line in the file does not have a terminating newline. If that is a concern, see the accepted answer to Read last line of file in bash script when reading file line by line for a fix.

Upvotes: 1

RavinderSingh13
RavinderSingh13

Reputation: 133750

Could you please try following(if ok with awk).

awk '/del/ && !/ins/'  Input_file

Upvotes: 2

Related Questions