Trying to get an awk script to replace values in column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trying to get an awk script to replace values in column
# 1  
Old 04-06-2017
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?

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


Last edited by jim mcnamara; 04-07-2017 at 12:10 AM..
# 2  
Old 04-07-2017
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

Last edited by Scrutinizer; 04-07-2017 at 12:31 AM..
# 3  
Old 04-07-2017
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:
Code:
#!/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. Smilie
# 4  
Old 04-07-2017
Okay, so for example, here is what is inside one of the Node text files-

Code:
-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-

Code:
-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.

Last edited by Scrutinizer; 04-07-2017 at 01:27 AM.. Reason: code tags
# 5  
Old 04-07-2017
Literal translation of the specification would result in something like this:
Code:
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
# 6  
Old 04-07-2017
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?

Last edited by Eric1; 04-07-2017 at 02:57 AM..
# 7  
Old 04-07-2017
You're welcome. You can modify the script like this:
Code:
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:
Code:
for filename in Node[1-4].txt
do
  mv "${filename}.new" "${filename}"
done


Last edited by Scrutinizer; 04-07-2017 at 03:06 PM.. Reason: Forgot quote; changed Node{1..4}.txt to Node[1-4].txt
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk script to extract a column, replace one of the header and replace year(from ddmmyy to yyyy)

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)
Discussion started by: Kunalcurious
10 Replies

2. UNIX for Beginners Questions & Answers

Replace a numeric values in a certain column

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)
Discussion started by: arunkumar_mca
3 Replies

3. Shell Programming and Scripting

awk script to append suffix to column when column has duplicated values

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)
Discussion started by: as7951
1 Replies

4. Shell Programming and Scripting

Do replace operation and awk to sum multiple columns if another column has duplicate values

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)
Discussion started by: as7951
12 Replies

5. Shell Programming and Scripting

Selective Replace awk column values

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)
Discussion started by: sdohn
3 Replies

6. Shell Programming and Scripting

for each different entry in column 1 extract maximum values from column 2 in unix/awk

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)
Discussion started by: Diya123
1 Replies

7. UNIX for Dummies Questions & Answers

Replace values in a specified column of a file

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)
Discussion started by: Gussifinknottle
4 Replies

8. Shell Programming and Scripting

How to pick values from column based on key values by usin AWK

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)
Discussion started by: repinementer
16 Replies

9. Shell Programming and Scripting

replace the column values.

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)
Discussion started by: charandevu
6 Replies

10. UNIX for Advanced & Expert Users

replace a column values with the first value in column

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)
Discussion started by: sumeet
3 Replies
Login or Register to Ask a Question