0

I'm trying to write a bash script which will submit as numerous simultaneous jobs. At one point in the script I want it to find a value from a specific cell in a table based on the job ID number.

The issue is that I am trying to use awk to return the value based on an using a sum in the awk command, which I cannot get to work.

The issue is that I cannot just use the Job ID as a variable because that will print the value one row up from the row I actually want (because awk row 1 will be the header). So I actually need row = Job_ID + 1

I think one potential solution is that I could store it in a variable like so:

counter=$((${SGE_TASK_ID}+1))

And then pass it as follows:

gene=awk -v counter=$counter 'FNR == $counter {print $2; exit}' "$mainDir"inputs/genelist.tsv

However, I am not certain whether this will work (as I am fairly inexperienced with awk) and also would like to ask for posterity whether there is a method of performing the addition within the awk command.

Something like:

gene=awk 'FNR == "$((${SGE_TASK_ID}+1))" {print $2; exit}' "$mainDir"inputs/genelist.tsv

The end result should be the value of the cell in row n+1 from column 2 in the file genelist.tsv

Thus far when using the addition method within awk it has simply been returning nothing, or an error saying that the command cannot be found.

Edit: It's maybe worth mentioning that in this case the variable of mainDir is defined earlier like so:

mainDir="/exports/lab/projects/prj_099/"
Sabor117
  • 111
  • 1
  • 11
  • 1
    You're missing the command substitution syntax. `gene=$(awk ...)` -- with the existing `gene=awk -v ...`, you're assigning `awk` as the value to a variable named `gene` while running a command called `-v`. – Charles Duffy Feb 07 '19 at 16:47
  • 1
    ...so at this point, the code given in the question demonstrates a shell bug, not an awk bug. If editing to try to correct that, please while you're at it attempt to generate a proper [mcve] -- code someone else can copy and paste with no additional setup to see the problems themselves. (That means setting variables inside the example instead of requiring someone else to intuit values for them, using heredocs instead of referring to files on-disk, or at least providing code that *creates* minimal versions of those files, etc) – Charles Duffy Feb 07 '19 at 16:49
  • ... a good way to make sure you have a standalone MCVE is to try to run it on an online, sandboxed interpreter such as that at http://ideone.com/ -- if it can be run there, anyone else here should be able to run it as well. – Charles Duffy Feb 07 '19 at 16:51
  • 1
    BTW, in `awk -v "counter=$counter" '...'`, the code in `...` can just refer to `counter`, not `$counter`, if you want to refer to the specific value that was passed in, as opposed to the contents of a field numbered by that value.. – Charles Duffy Feb 07 '19 at 16:52
  • Oh, I had forgotten about this part of the problem as well... It's worth saying that I was getting the errors as well when using the format of: ```awk 'FNR == ...``` (I.e. when not trying to assign the output as a variable). – Sabor117 Feb 07 '19 at 16:55
  • Also, I appreciate the feedback about the question. I will admit though that I find it difficult to know exactly how much information to supply sometimes (I mean, this seemed like a very basic question so I assumed providing examples of the data wouldn't really be necessary). – Sabor117 Feb 07 '19 at 16:57
  • 1
    Typically, it's better to simplify your code so it doesn't *need* data at all, if you can generate simpler code that still shows the exact question you're trying to ask for help with. Posting code that *requires* data, without including that data, makes it hard for people to reproduce the problem or test their answers. See also the "Tricks for Trimming" section at http://sscce.org/ – Charles Duffy Feb 07 '19 at 16:58

1 Answers1

1

just do the math in awk

e.g.

$ awk -v id="$SGE_TASK_ID" 'FNR==(id+1){print $2; exit}' file
karakfa
  • 66,216
  • 7
  • 41
  • 56
  • Right, so the important aspect here is that to perform the maths like I want inside awk I will need to actually assign my shell variable to another awk variable first? Thanks very much! – Sabor117 Feb 07 '19 at 16:59
  • 1
    yes, shell variables cannot pass the single quote barrier. – karakfa Feb 07 '19 at 16:59
  • 1
    *nod*. You *can* still pass the result of shell math to awk -- `awk -v id="$(( SGE_TASK_ID + 1 ))" 'FNR == id { print $2; exit; }'` would work, but awk is better at math than the shell is, so the approach given in this answer is generally preferable (works in more scenarios, faster to execute, etc). – Charles Duffy Feb 07 '19 at 17:00