1

For instance, I have the following in a tab separated text file with two columns:

blah    ABC_DE_23154_e53G_12
blah    DEF_GH_23165_f35H_36
blah    CED_BF_56412_c56T_21

I am using the following awk script to extract the lines that matches the third number in the second column falling in a specific range (between 23153 and 23167) after the second underscore (which doesn't work):

awk -F "_" '$2>23153 && $2<23167' *.txt >> output.txt

Output inside the "output.txt" file should be:

blah    ABC_DE_23154_e53G_12
blah    DEF_GH_23165_f35H_36

I have around 600 text files with around 8000 lines of data in each file.

Thanks, plasma33

3
  • 1
    Can blah ever contain a _? – Ed Morton Jan 25 '20 at 21:38
  • It doesn't, thanks for checking. However, it would be great to have a work around that. – user3441801 Jan 28 '20 at 15:18
  • If you want a solution that includes a workaround for that then you should include that in your sample input/output so we have something to test against that produces a pass/fail result for input that includes that case. – Ed Morton Jan 28 '20 at 15:19
2

I think the first problem is $2 in your awk script, because with $2 is the second column (DE, GH, BF, ...), not the third one where the numbers you want to compare are.

Then, there should be a condition and what you want to do if the condition is met.

awk -F'_' '($3>23153 && $3<23167){print}' *.txt >> output.txt

There is a condition in () and action in {}.

EDIT:

As I've been reminded in the comments, {print} action is the default one, so you can further simplify the awk script to:

awk -F'_' '($3>23153 && $3<23167)' *.txt >> output.txt

0
1

Tried with Python

k=open('filename','r')
for i in k:
    k=i.strip().split('_')[2]
    if int(k) > 23153<23167:
        print i.strip()

output

blah    ABC_DE_23154_e53G_12
blah    DEF_GH_23165_f35H_36
blah    CED_BF_56412_c56T_21
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.