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?
Last edited by jim mcnamara; 04-07-2017 at 12:10 AM..
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
Last edited by Scrutinizer; 04-07-2017 at 12:31 AM..
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:
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.
Okay, so for example, here is what is inside one of the Node text files-
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-
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.
Last edited by Scrutinizer; 04-07-2017 at 01:27 AM..
Reason: code tags
Literal translation of the specification would result in something like this:
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?
I have a csv which has lot of columns . I was looking for an awk script which would extract a column twice. for the first occurance the header and data needs to be intact but for the second occurance i want to replace the header name since it a duplicate and extract year value which is in ddmmyy... (10 Replies)
Hi All,
I am trying to replace a certain value from one place in a file . In the below file at position 35 I will have 8 I need to modify all 8 in that position to 7
I tried
awk '{gsub("8","7",$35)}1' infile > outfile ----> not working
sed -i 's/8/7'g' infile --- it is replacing all... (3 Replies)
Please help me to get required output for both scenario 1 and scenario 2 and need separate code for both scenario 1 and scenario 2
Scenario 1
i need to do below changes only when column1 is CR and column3 has duplicates rows/values. This inputfile can contain 100 of this duplicated rows of... (1 Reply)
Hi Experts,
Please bear with me, i need help
I am learning AWk and stuck up in one issue.
First point : I want to sum up column value for column 7, 9, 11,13 and column15 if rows in column 5 are duplicates.No action to be taken for rows where value in column 5 is unique.
Second point : For... (12 Replies)
Hi, I have the following data:
2860377|"DATA1"|"DATA2"|"65343"|"DATA2"|"DATA4"|"11"|"DATA5"|"DATA6"|"65343"|"DATA7"|"0"|"8"|"1"|"NEGATIVE"
32340377|"DATA1"|"DATA2"|"65343"|"DATA2"|"DATA4"|"11"|"DATA5"|"DATA6"|"65343"|"DATA7"|"0"|"8"|"1"|"NEG-DID"... (3 Replies)
Hello,
I have 2 columns (1st column has multiple entries but the corresponding values in the column 2 may be the same or different.) however I want to extract unique values for each entry in column 1 by assigning the max value from column 2
SDF4 -0.211654
SDF4 0.978068
... (1 Reply)
Hello,
I have a file with four columns and I would like to replace values in the second column only.
An arbitrary example is:
100 A 105 B
200 B 205 C
300 C 305 D
400 D 405 E
500 E 505 F
I need to replace the second column as shown below:
... (4 Replies)
Dear Guyz:)
I have 2 different input files like this. I would like to pick the values or letters from the inputfile2 based on inputfile1 keys (A,F,N,X,Z).
I have done similar task by using awk but in that case the inputfiles are similar like in inputfile2 (all keys in 1st column and values in... (16 Replies)
I have the below file ...where some of the column values should replaced with desired values ....below file u can find that 3 column where ever 'AAA' comes should replaced with ' CC '
NOTE : we have to pass the column number ,AAA,CC (modified value) as the parameters to the code.
... (6 Replies)
Hi All,
I have a file which has data in following format:
"Body_Model","2/1/2007","2/1/2007"
"CSCH74","0","61"
"CSCS74","0","647"
"CSCX74","0","3"
"CSYH74","0","299"
"CSYS74","0","2514"
"CSYX74","0","3"
"Body_Model","3/1/2007","3/1/2007"
"CSCH74","0","88"
"CSCS74","0","489"... (3 Replies)