1

I want to delete all lines in which MGD is not between 676 and 900. I wrote a script

#!/bin/bash
for index in {1..100} # I do this script on 100 files, that is why I use for loop
do
awk 'BEGIN { FS = "MGD" };
        {if ($2 >= 676 && $2 <= 900) print}' eq2_15_333_$index.ndx | tee eq3_15_333_$index.ndx
done

Input example

MGD816 SOL77
MGD71 SOL117
MGD7 SOL13194
MGD18 SOL235
MGD740 SOL340
MGD697 SOL396
MGD70 SOL9910

Expected output

MGD816 SOL77
MGD740 SOL340
MGD697 SOL396

I don't know what my script do something wrong, because I still have something which has MGD7 or MGD71, but MGD18 I haven't in my output.

Edit

I tested this script and it works perfectly

   awk '/^MGD/{val=substr($1,4);if(val+0 >= 676 && val+0 <= 900){print}}' new.txt | tee new2.txt

and I have output

MGD816 SOL77
MGD740 SOL340
MGD697 SOL396
Jakub
  • 679
  • 5
  • 16
  • 1
    As I always point out in your posts (but I'm trying to stop myself!) - you don't need a shell loop for this and it'd literally run orders of magnitude faster if you didn't. – Ed Morton Feb 03 '21 at 16:07
  • @EdMorton Thanks for advice So how to do this example without for loop? Is it possible? – Jakub Feb 03 '21 at 16:36
  • 1
    See our conversation at https://stackoverflow.com/questions/65980078/problem-with-the-save-changes-in-the-same-file-with-awk#comment116656232_65980078 - nothing has changed. – Ed Morton Feb 03 '21 at 20:08
  • @EdMorton I try again with my question. I hope now is ok? Thanks in advance https://stackoverflow.com/questions/65980443/instead-using-awk-in-for-loop-in-bash-through-all-files-do-something-only-in-a – Jakub Feb 04 '21 at 13:59

2 Answers2

2

Based on your shown samples try following once. This is completely based on your shown attempt, since no samples were provided so its not tested, should work though.

#!/bin/bash
for index in {1..100}
do
   awk '/^MGD/{val=substr($1,4);if(val+0 >= 676 && val+0 <= 900){print}}' eq2_15_333_$index.ndx | tee eq3_15_333_$index.ndx
done
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • I edited the first post and try your script and test this script, but its not working. I still have MGD between MGD7-9 and between MGD70-89 probably – Jakub Feb 03 '21 at 13:48
  • 1
    @Mark, oh ok, my apologies, please try my edited code now? – RavinderSingh13 Feb 03 '21 at 13:51
  • 1
    Now it works perfect. Thank you so much. So I should "+0" to have properly output – Jakub Feb 03 '21 at 13:54
  • 1
    @Mark, your welcome(yes +0 will do the trick), also you could revert your question to your actual question else it may confuse users(where you added my suggested command is not working there), cheers. – RavinderSingh13 Feb 03 '21 at 13:56
1

I want to explain why your original i.e.

awk 'BEGIN { FS = "MGD" }; {if ($2 >= 676 && $2 <= 900) print}'

did not work as excepted, you set "MGD" as FS thus AWK splitted only at MGD - if you do awk 'BEGIN{FS="MGD"}{print $2}' file.txt and content of file.txt is

MGD816 SOL77
MGD71 SOL117
MGD7 SOL13194
MGD18 SOL235
MGD740 SOL340
MGD697 SOL396
MGD70 SOL9910

output is

71 SOL117
7 SOL13194
18 SOL235
740 SOL340
697 SOL396
70 SOL9910

If you want $2 to be first number you should specify FS which is "MGD" or spaces i.e.

awk 'BEGIN{FS="MGD|[[:space:]]+"}...

Daweo
  • 31,313
  • 3
  • 12
  • 25