1

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
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
justaguy
  • 2,908
  • 4
  • 17
  • 36

5 Answers5

2

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

awk '/del/ && !/ins/'  Input_file
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
1

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.

pjh
  • 6,388
  • 2
  • 16
  • 17
1

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

grep "del" file | grep -v "ins"
Walter A
  • 19,067
  • 2
  • 23
  • 43
1

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
iamauser
  • 11,119
  • 5
  • 34
  • 52
1

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
iamauser
  • 11,119
  • 5
  • 34
  • 52