Trying to get an awk script to replace values in column

I'm trying to make an awk script to compare values I've set as var1, var2, and var3 earlier in the script to the values in the userinputted column of four text files called Node1.txt, Node2.txt, Node3.txt, and Node4.txt and then replace the values in that userinputted column with either ttt or gcc, based on the two conditions shown below. The script runs without any hitches but it is not actually changing anything in those text files, can someone help explain why and what I need to alter or change?

awk '{
if ($userinput < $var3 && $userinput > $var)
        awk '${userinput="ttt"}'
elif ($userinput >= $var3 && $userinput < $var2)
         awk '${userinput="gcc"}'
}' Node {1..4}.txt

Hi,

  • The script is trying to use shell variables inside awk; that will not work
  • The script does not write anything to file(s), so nothing will be altered at the file level
  • awk is called within awk, that is not possible
  • The script does not do anything
  • The only thing that effectively gets executed are ${userinput="ttt"} and then ${userinput="gcc"} , which runs as shell code and results in setting the variable userinput to ttt if it was not set before..

Can you show us what you are trying to accomplish by posting some sample input and output and state you OS and version

Your awk code cannot work the way it is.
To pass variables in to an awk script:
the syntax is
awk -v variablename="value"
Example:

#!/bin/bash
var1="hi there"
awk -v var="$var1" '{print var, $0}' myinputfile

You need 3 of these: one for var var2 and var3. You canruse the names var1, var2, and var3 for the awk internal variables to make your code clear; awk internal variables are unaware of shell variables.

Note {1..4}.txt ->
indicates five files to read, one named Node, 4 named [number].txt - none of these will ever be written to.

Suggestion - Please do not show us what you want to code, lets us see what you are trying to do. Your code is really, um, impossible as presented. There are so many other things that can not work.

-- Edit: Scrutinizer said the same things. :slight_smile:

Okay, so for example, here is what is inside one of the Node text files-

-80.0222 -81.3604 -83.2733 -87.9688 -86.7132 -82.938 -81.6501 -76.9167 -78.9644 -80.2246 
-79.0461 -80.5249 -82.2464 -86.497 -85.8544 -82.4721 -81.4323 -77.8798 -80.7688 -80.987 
-77.9511 -79.6907 -82.1187 -85.737 -84.9415 -81.189 -80.1036 -77.4193 -80.762 -81.287 
-77.4729 -78.9058 -81.1581 -84.3832 -85.2848 -81.6571 -79.811 -77.7476 -81.1131 -81.4729 
-77.2045 -78.5694 -80.4245 -84.6326 -85.4181 -81.7013 -79.8357 -78.016 -81.6587 -81.8589 

The input would simply be the user inputting an integer from 1 to 10, which is stored as "userinput" and for now we'll say they input 3, for column 3.
I have a number attached to var1-3, var1 in this case being -86.4149, var2 being -79.6892, and var3 being 3.

I want to replace the values in column 3 in this file along with the other three node files by comparing the values in the column with the numbers attached to var1, var2, and var3. If the values in the column are less than var3 and greater than var1, replace the values in the column with "ttt"

If the values in the column are greater than or equal to var 3 and less than var2, replace the values in the column with "gcc"

So basically a desired result would be this in the case of this example-

-80.0222 -81.3604 ttt -87.9688 -86.7132 -82.938 -81.6501 -76.9167 -78.9644 -80.2246 
-79.0461 -80.5249 ttt -86.497 -85.8544 -82.4721 -81.4323 -77.8798 -80.7688 -80.987 
-77.9511 -79.6907 ttt -85.737 -84.9415 -81.189 -80.1036 -77.4193 -80.762 -81.287 
-77.4729 -78.9058 ttt -84.3832 -85.2848 -81.6571 -79.811 -77.7476 -81.1131 -81.4729 
-77.2045 -78.5694 ttt -84.6326 -85.4181 -81.7013 -79.8357 -78.016 -81.6587 -81.8589 

Also I should note I made an error when referring to the Node files in that code. It should be Node{1..4}.txt not Node {1..4}.txt, the space there was an error on my part, my apologies.

Is this sufficient enough to get my idea across? If any more clarification is needed let me know. I apologize for my stupidity, I'm very new to bash scripting and this forum as well.

Literal translation of the specification would result in something like this:

userinput=3

awk -v var1=-86.4149 -v var2=-79.6892 -v var3=3 -v ui="$userinput" '
  $ui > var1 && $ui < var3 { 
    $ui="ttt"
  }

  $ui < var2 && $ui >= var3 {
    $ui="gcc" 
  }

  {
    print
  }
' Node{1..4}.txt

However, note that the value "gcc" will never be used, since a number cannot be both < -79.6892 and >= 3

Okay so first off, I would like to thank you for the reply and your assistance. I've inputted in the code you gave me with just some simple changes (like excluding the print line, as I don't want the script to print the newly changed text files, just simply change it is all) but when I try to run it it gives me an error that says "fatal: attempt to access field -86"

What could be the issue here?

EDIT: I went back and included the print line just in case that could be the issue but it still gives me this error when I try to run the script
EDIT2: Wait nevermind, I'm an idiot, my apologies. The script is working, I just made an error on my part. However, I don't really want the script to print out the results, but just simply change the values in the user-specified column of the Node files, so how would I fix that little aspect?

You're welcome. You can modify the script like this:

userinput=3

awk -v var1=-86.4149 -v var2=-79.6892 -v var3=3 -v ui="$userinput" '
  $ui > var1 && $ui < var3 { 
    $ui="ttt"
  }

  $ui < var2 && $ui >= var3 {
    $ui="gcc" 
  }

  {
    print > (FILENAME ".new")
  }
' Node{1..4}.txt

Which will create 4 new files

But what about < -79.6892 and >= 3 ?

--
If you are satisfied with the results, you can rename them to their original names:

for filename in Node[1-4].txt
do
  mv "${filename}.new" "${filename}"
done

This problem is VERY similar to this one which you admitted was homework.

If this here again is homework, PLEASE, seriously, follow the rules and use the homework form. If its not, again, show us the background of the problem, e.g. the company branch / project you work for, or the type of research.

@RudiC I don't work for any company or project, this is just me trying to learn

@Scrutinizer I tried to do it like this-

userinput=3
awk -v var1=-86.4149 -v var2=-79.6892 -v var3=3 -v ui="$userinput" '
  $ui > var1 && $ui < var3 { 
    $ui="ttt"
  }
  $ui < var2 && $ui >= var3 {
    $ui="gcc" 
  }
  {
    print > (NewNode ".new)
  }
' Node{1..4}.txt

But it gives me two errors. Both are targeting the quotation mark and one error says "unterminated string" and another says "syntax error"

---------- Post updated at 04:44 PM ---------- Previous update was at 04:43 PM ----------

@Scrutinizer so I checked out your edits and the code runs, but then when I check my directory I see no new files at all

EDIT: Wait nevermind, I'm an idiot, it's working, thank you.