Replacing field based on the value of other field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing field based on the value of other field
# 1  
Old 01-12-2018
Replacing field based on the value of other field

Hi Team,

In the below input file, if I have the value 23,24,25 then for those records 1st field value should get updated from "a" to "b". I also want to pass these values in file as input as it can be done dynamically. Tried awk commands but not getting desired output.Using SunOS 5.10 version. Thanks in advance.

#############################

Input


Code:
a,test,xyz,23,test
a,test2,abc,24,test
a,test3,def,25,test
a,test4,ghe,26,test

Output

Code:
b,test,xyz,23,test
b,test2,abc,24,test
b,test3,def,25,test
a,test4,ghe,26,test

#####################################

Last edited by Corona688; 01-12-2018 at 03:24 PM..
# 2  
Old 01-12-2018
Using nawk:-
Code:
nawk -F, '$4>=23&&$4<=25{$1="b"}1' OFS=, file

This User Gave Thanks to Yoda For This Post:
# 3  
Old 01-12-2018
Thanks Yoda it works. Can I search any filed which has 23,24,25 and not specifically $4 and replace $1 with "b". Just curious to know if we can do that.
# 4  
Old 01-12-2018
Code:
nawk -F, '
        {
                for ( i = 1; i <= NF; i++ )
                {
                        if ( $i >= 23 && $i <= 25 )
                        {
                                $1 = "b"
                        }
                }
        }
        1
' OFS=, file

# 5  
Old 01-12-2018
You can use sed too
For the first requirement
Code:
sed -E 's/([^,]*)(,[^,]*,[^,]*,)(2[345],)(.*)/b\2\3\4/' infile

If a is always the first field
Code:
sed -E 's/a(,[^,]*,[^,]*,)(2[345],)(.*)/b\1\2\3/' infile

For the second requirement
Code:
sed '/,2[345],/s/[^,]*/b/' infile

If a is always the first field
Code:
sed '/,2[345],/s/^a,/b,/' infile

Not tested on SunOS 5.10 version
# 6  
Old 01-12-2018
Thanks Yoda and Ctac. Command line is working fine. But file has field with double quotes. With the double quotes the desired output is not coming.

################################################

Input


Code:
a,test,xyz,"23",test
a,test2,abc,"24",test
a,test3,def,"25",test
a,test4,ghe,"26",test

Output

Code:
b,test,xyz,"23",test
b,test2,abc,"24",test
b,test3,def,"25",test
a,test4,ghe,"26",test


Last edited by Corona688; 01-12-2018 at 03:24 PM..
# 7  
Old 01-12-2018
Please use code tags for code. Select the text then hit the Image button.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Problem with getting awk to multiply a field by a value set based on condition of another field

Hi, So awk is driving me crazy on this one. I have searched everywhere and read man, docs and every related post Google can find and still no luck. The actual files I need to run this on are sensitive in nature, but it is the same thing as if I needed to calculate weighted grades for multiple... (15 Replies)
Discussion started by: cotilloe
15 Replies

2. Shell Programming and Scripting

awk to adjust coordinates in field based on sequential numbers in another field

I am trying to output a tab-delimited result that uses the data from a tab-delimited file to combine and subtract specific lines. If $4 matches in each line then the first matching sequential $6 value is added to $2, unless the value is 1, then the original $2 is used (like in the case of line... (3 Replies)
Discussion started by: cmccabe
3 Replies

3. Shell Programming and Scripting

awk to update value in field based on another field

In the tab-delimeted input file below I am trying to use awk to update the value in $2 if TYPE=ins in bold, by adding the value of HRUN= in italics. In the below since in line 1 TYPE=ins the 117282541 value in $2 has 6 added because that is the value of HRUN=. Hopefully the awk is a start but I... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

replacing a value in a field

Hi All, I have a file yum.conf that has a field called gpgcheck this field sometimes has a value of 0 gpgcheck=0 and at other times it has a 1. I need to check the value and if it is a 1 change it to a 0 any ideas? thanks, Gartie (1 Reply)
Discussion started by: gartie
1 Replies

5. Shell Programming and Scripting

Replacing the last field

Suppose I have a file abc.txt which contain lines:- 11.3.5.7 11.3.6.7 11.6.8.9.10 I want to replace the last field of the line to some value .I want the first line should become 11.3.5.86 .Same applies for rest lines.Please help. (6 Replies)
Discussion started by: maitree
6 Replies

6. Shell Programming and Scripting

Help with Awk finding and replacing a field based on a condition

Hi everybody, I'm trying to replace the $98 field with "T" if the last field (108th) is T I've tried awk 'BEGIN{OFS=FS="|"} {if ($108=="T")sub($98,"T"); print}' test.txt but that doesn't do anything also tried awk 'BEGIN{OFS=FS="|"}{ /*T.$/ sub($98,"T")} { print}' test.txt but... (2 Replies)
Discussion started by: jghi123
2 Replies

7. UNIX for Dummies Questions & Answers

awk - Summing a field based on another field

So, I need to do some summing. I have an Apache log file with the following as a typical line: 127.0.0.1 - frank "GET /apache_pb.gif HTTP/1.0" 200 2326 Now, what I'd like to do is a per-minute sum. So, I can have awk tell me the individual minutes, preserving the dates(since this is a... (7 Replies)
Discussion started by: treesloth
7 Replies

8. Shell Programming and Scripting

Find top N values for field X based on field Y's value

I want to find the top N entries for a certain field based on the values of another field. For example if N=3, we want the 3 best values for each entry: Entry1 ||| 100 Entry1 ||| 95 Entry1 ||| 30 Entry1 ||| 80 Entry1 ||| 50 Entry2 ||| 40 Entry2 ||| 20 Entry2 ||| 10 Entry2 ||| 50... (1 Reply)
Discussion started by: FrancoisCN
1 Replies

9. Shell Programming and Scripting

need help with replacing a certain field...

Hi, can anyone help me? This is what i want to do....I have a string UNB+UNOA:1+OOCLIES+RTTC+080408:0358+1' and i want to replace the "1" at the end (that specific field only) to 00001 such that the new output will be like this UNB+UNOA:1+OOCLIES+RTTC+080408:0358+00001' i tried using... (5 Replies)
Discussion started by: shennanigan83
5 Replies

10. Shell Programming and Scripting

Replacing certain field

hi all, i am having a script file which contains lot of fields and commands. let's say i have the following word (example1) spread all over the script in every place, how can i replace it with the word (example2)?....the only way i know is to use to either delete the word (example1) and write... (1 Reply)
Discussion started by: charbel
1 Replies
Login or Register to Ask a Question