AWK How to replace a field using 2 shell variables?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK How to replace a field using 2 shell variables?
# 1  
Old 07-28-2011
AWK How to replace a field using 2 shell variables?

Hello everybody:
I want to replace any field $2 of any file line (f.i. test.txt) matching $1 with a shell variable.

Code:
$ cat test.txt    
F       0
B       A
H       -12.33

Now I'm going to ask the value of variable B:
Code:
$ SEARCHVAR=B
$ OLDVAL=$(awk -v SEARCHVAR="$SEARCHVAR" '$1==SEARCHVAR {print $2}' test.txt)
$ echo $OLDVAL
A

And finally i tries to replace this value ("A") from field 2 by another shell variable with "10", using awk again:
Code:
$ NEWVAL=10
$ awk -v SEARCHVAR="$SEARCHVAR" OLDVAL="$OLDVAL" NEWVAL="$NEWVAL" '{if($1==SEARCHVAR && $2==OLDVAL) sub{OLDVAL,NEWVAL,$2} {print}}' test.txt

Unfortunately I only gets fatal error messages again & again.
Can you help me, please?
Thanks all in advace.

Last edited by basalt; 07-28-2011 at 04:42 PM..
# 2  
Old 07-28-2011
Code:
# cat file
F 0
B A
H -12.33
# awk -vSEARCHVAR="B" -vOLDVAL="A" -vNEWVAL=10 '$1==SEARCHVAR && $2==OLDVAL{sub(OLDVAL,NEWVAL,$2)}1' file
F 0
B 10
H -12.33

PS: Please use [code] tags when you post code or data sample.
This User Gave Thanks to danmero For This Post:
# 3  
Old 07-28-2011
I just edited it with codes danmero.

Your solution was not able for me. I can't to look the old value from a file every time i needs to change it. I must that the shell do it by itself. But thanks to it, finally I found with the solution:
Code:
#!/bin/bash
SEARCHVAR=B
NEWVAL=10
OLDVAL=$(awk -v SEARCHVAR="$SEARCHVAR" '$1==SEARCHVAR {print $2}' test.txt)
awk -vSEARCHVAR="$SEARCHVAR" -vOLDVAL="$OLDVAL" -vNEWVAL="$NEWVAL" '$1==SEARCHVAR && $2==OLDVAL{sub(OLDVAL,NEWVAL,$2)}1' test.txt

the shell will show this:
Code:
A       0
B 10
C       -12.33

I hope this can to help it to someone Smilie

Last edited by basalt; 07-28-2011 at 06:23 PM..
# 4  
Old 07-28-2011
Code:
# cat file
F       0
B       A
H       -12.33
# SEARCHVAR=B
# NEWVAL=10
# awk -vSEARCHVAR="$SEARCHVAR" -vNEWVAL="$NEWVAL" '$1==SEARCHVAR{$2=NEWVAL}1' OFS=\\t file
F       0
B       10
H       -12.33

If that's what you want , you don't need to parse the file twice.
This User Gave Thanks to danmero For This Post:
# 5  
Old 07-29-2011
Quote:
Originally Posted by danmero
Code:
# cat file
F       0
B       A
H       -12.33
# SEARCHVAR=B
# NEWVAL=10
# awk -vSEARCHVAR="$SEARCHVAR" -vNEWVAL="$NEWVAL" '$1==SEARCHVAR{$2=NEWVAL}1' OFS=\\t file
F       0
B       10
H       -12.33

If that's what you want , you don't need to parse the file twice.
Oh yes!! That is an awsome solution. Thanks a lot ! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to assign points to variables based on conditions and update specific field

I have been reading old posts and trying to come up with a solution for the below: Use a tab-delimited input file to assign point to variables that are used to update a specific field, Rank. I really couldn't find too much in the way of assigning points to variable, but made an attempt at an awk... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

awk to replace a specific field in certain condition

Hi, I have a file like below PRUM,67016800 ,CC ,C1,67016800 , ,Y,Y,2 ,CK,BX,FOX ,00000001,EA,00000001,20141120 00:00:00, ,N,Y,Y,CK ABCDEF... (7 Replies)
Discussion started by: mady135
7 Replies

3. Shell Programming and Scripting

awk Match First Field and Replace Second Column

Hi Friends, I have looked around the forums and over online but couldn't figure out how to deal with this problem input.txt gene1,axis1/0/1,axis2/0/1 gene1,axis1/1/2,axis2/1/2 gene1,axis1/2/3,axis2/2/3 gene2,axis1/3/4,axis2/3/4 Match on first column and if first column is... (1 Reply)
Discussion started by: jacobs.smith
1 Replies

4. Shell Programming and Scripting

AWK replace one field

I have some problem with this. Need to change field #3 to 5 if field #1 = A and filed #2 =B DataA C 6 T C B 4 R A B 3 T D E 5 4 I would like to do two things if statement is true, but can not get it to work. Here it prints column #3 if statement is true, and this works awk '{if... (3 Replies)
Discussion started by: Jotne
3 Replies

5. Shell Programming and Scripting

awk search/replace specific field, using variables for regexp & subsitution then overwrite file

Hello, I'm trying the solve the following problem. I have a file which I intend to use as a csv called master.csv The columns are separated by commas. I want to change the text on a specific row in either column 3,4,5 or 6 from xxx to yyy depending upon if column 1 matches a specified pattern.... (3 Replies)
Discussion started by: cyphex
3 Replies

6. Shell Programming and Scripting

awk how to replace specific field with new value

I need to replace specific field (x) in a table with new value (y): Input: 1 2 3 4 5 x 6 7 8 9 0 0 Output: 1 2 3 4 5 y 6 7 8 9 0 0 I have no idea how to do this. (10 Replies)
Discussion started by: setepo
10 Replies

7. Shell Programming and Scripting

awk search and replace field

I am writing a c++ program that has many calls of pow(input,2). I now realize that this is slowing down the program and these all should be input * input for greater speed. There should be a simple way of doing this replacement throughout my file with awk, but I am not very familiar with awk.... (2 Replies)
Discussion started by: bluejayek
2 Replies

8. Shell Programming and Scripting

Awk to replace a field

Hi I am using awk to replace the 4th feild of the input line. My code is below: REP_LINE=$(echo $Line | awk -v var=$REPL_DT '{$4=var; print}') It does replaces but all words/phrases after the 4th feild are erased. Is there a way to modify the 4th feild of the input line and to have all... (7 Replies)
Discussion started by: sugan
7 Replies

9. Shell Programming and Scripting

awk to replace particular field

i have bad data for instance, the use of capital letters in the middle of the word. I identified the errors, made a list and put it in a file error_correction.txt Incorrect,Correct VeNOM,Venom nos,NOS . . . My data file looks like this: vgr,bugatti veron,,3.5,Venom,6,.......,....... (14 Replies)
Discussion started by: VGR
14 Replies

10. Shell Programming and Scripting

search and replace using awk with variables

Hi, I have been trying to use awk with variables that needs to search for a pattern and replace it with another pattern, the patterns are supplied in a variable. I have tried several different ways without success and hope that someone can help me. Here are the details echo $UPC 07007457809... (2 Replies)
Discussion started by: jerardfjay
2 Replies
Login or Register to Ask a Question