0

This is my whole script

#!/bin/bash
num_autocorellation=1
index_min=1
index_max=5000
for first in {1..10000..100}
do
for index in {$index_min..$index_max} 
do
    awk 'NR==FNR{a[$0]++;next}a[$0]-->0' eq3_900_915_dgdg_$first.ndx eq3_900_915_dgdg_$index.ndx | tee eq4_${num_autocorellation}_900_915_dgdg_$index.ndx
done
index_min=$(($index_min+100))
index_max=$(($index_max+100))
num_autocorellation=$(($num_autocorellation+1))
done

I want to have in the beginning in my nested loop something like that

for index in {1..5000} 
do
    awk 'NR==FNR{a[$0]++;next}a[$0]-->0' eq3_900_915_dgdg_1.ndx eq3_900_915_dgdg_$index.ndx | tee eq4_1_900_915_dgdg_$index.ndx
done

Then after this for loop will end I want to have

for index in {100..5100} 
do
    awk 'NR==FNR{a[$0]++;next}a[$0]-->0' eq3_900_915_dgdg_1.ndx eq3_900_915_dgdg_$index.ndx | tee eq4_2_900_915_dgdg_$index.ndx
done

and again again until 10000 in that loop

for first in {1..10000..100}

But I have a problem in this part

for index in {$index_min..$index_max} 

Instead of integer I have in the file something like that

eq4_1_900_905_dgdg_{1..5000}.ndx

but I want to have

eq4_1_900_905_dgdg_1.ndx

So how to put here

for index in {$index_min..$index_max} 

int variables?

Jakub
  • 679
  • 5
  • 16
  • Check if this helps: https://stackoverflow.com/questions/17181787/how-to-use-variables-in-a-bash-for-loop – samthegolden Feb 17 '21 at 15:52
  • What are you trying to do, print different ranges of lines to different files? – Raman Sailopal Feb 17 '21 at 16:04
  • @RamanSailopal print lines that are the same in both files (repetitions are important, If I have 3 the same lines in the first file and two the same lines In second file in output I will print 2 lines) – Jakub Feb 17 '21 at 16:06
  • 1
    If you are comparing line by line, use something like "comm" – Raman Sailopal Feb 17 '21 at 16:17
  • `{$index_min..$index_max}` won't do what you think it does. – M. Nejat Aydin Feb 17 '21 at 16:45
  • It's disappointing to see you're back to calling awk one file at a time in shell loops again after https://stackoverflow.com/a/66048733/1745001 and https://stackoverflow.com/a/66141743/1745001. – Ed Morton Feb 18 '21 at 02:30
  • @EdMorton But I must have my output in a separate file. I can't use inplace (save my output in the same file, because I will do other things on my input files and I can't lose the information in my input file. If you know how can I do this without losing my input I will do this. Inplace and "eq6_{1..2}.ndx &&" would replace input information to output information. – Jakub Feb 18 '21 at 12:46
  • 1
    Using `-i inplace` is never necessary. Both of my answers mentioned in my previous comment showed you how to create a new file for each input file and https://stackoverflow.com/questions/66141434/substarct-bash-variable-as-a-name/66141743#66141743 specifically does what you want to do here - generate `eq4foo` files from `eq3foo` files while also displaying the output to stdout. – Ed Morton Feb 18 '21 at 13:26
  • @EdMorton Thanks a lot for the advice. I have a question. You write here https://stackoverflow.com/questions/66141434/substarct-bash-variable-as-a-name/66141743#66141743 only this "eq3_dgdg_{1..5001}.ndx" and this will only replace (in this thread I don't see that you write output to the different file), so how to write to output? I should use something like that? "eq3_dgdg_{1..5001}.ndx | tee eq4_dgdg_{1..5001}.ndx"? – Jakub Feb 18 '21 at 14:01
  • 1
    `print > out` in that script is writing the output to a file whose name is stored in the variable `out` that is set earlier in the script to the input file name but with `3` changed to `4` (see `out = FILENAME; sub(/3/,"4",out)`). The other `print` above it is to mimic `tee`. – Ed Morton Feb 18 '21 at 14:03
  • 1
    By the way, I notice in all of your scripts you use `cmd infile | tee outfile` - is that because you truly do want to get the output to display on stdout as well as be written to `outfile` or is it just that that's the only way you know to write the output to `outfile`? If you don't want the output to go to stdout too you then you'd just do `cmd infile > outfile` – Ed Morton Feb 18 '21 at 14:06
  • Now I see, I didn't notice that. To be honest @EdMorton that's the only way I know (sometimes is good to see my output on terminal, but I would say this is a side effect). – Jakub Feb 18 '21 at 14:10
  • 1
    OK, that's what I suspected. So then you don't need the 2nd `print` on it's own in my script, just `print > out`. – Ed Morton Feb 18 '21 at 14:11
  • 1
    Also btw with respect to `awk 'NR==FNR{a[$0]++;next}a[$0]-->0'`. You're using the array to keep a **count** so it'd be more useful to people reading your code if you named it `count[]` or `cnt[]` or even `c[]` rather than `a[]`. Also, `c[$0]-->0` will fail if your input file is so large that the value being decremented wraps around to become positive again since you don't stop decrementing when it hits zero. Use `c[$0] && (c[$0]-- > 0)` to guard against that, see https://stackoverflow.com/questions/17908555/printing-with-sed-or-awk-a-line-following-a-matching-pattern/17914105#17914105. – Ed Morton Feb 18 '21 at 14:15

0 Answers0