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

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

Output

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

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

Using nawk:-

nawk -F, '$4>=23&&$4<=25{$1="b"}1' OFS=, file
1 Like

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.

nawk -F, '
        {
                for ( i = 1; i <= NF; i++ )
                {
                        if ( $i >= 23 && $i <= 25 )
                        {
                                $1 = "b"
                        }
                }
        }
        1
' OFS=, file

You can use sed too
For the first requirement

sed -E 's/([^,]*)(,[^,]*,[^,]*,)(2[345],)(.*)/b\2\3\4/' infile

If a is always the first field

sed -E 's/a(,[^,]*,[^,]*,)(2[345],)(.*)/b\1\2\3/' infile

For the second requirement

sed '/,2[345],/s/[^,]*/b/' infile 

If a is always the first field

sed '/,2[345],/s/^a,/b,/' infile

Not tested on SunOS 5.10 version

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

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

Output

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

Please use code tags for code. Select the text then hit the button.

The requirement change ?:confused:
Just add " around 2[345]

Thanks ctac. It works.

sed '/,2[345],/s/[^,]*/b/' infile

Can we seperate the multiple patterns because I do have other list of values(34,38,100,1566) which needs to be replaced.

Thanks for your help

Yes we can.
But give a representative input and expected output.
And give also what you have try to resolve your problem.

Thanks ctac.

Am just trying to give multiple pattern search which is if the values are 23,24,25,1003,998,14356 then $1 should be replaced with b.

I tried below and its not working

sed '/,2[345][1003][998][14356],/s/[^,]*/b/' infile

Input -

a,test,xyz,"23",test
a,test2,abc,"24",test
a,test3,def,"25",test
a,test3,def,"1003",test
a,test3,def,"998",test
a,test3,def,"14356",test
a,test4,ghe,"26",test
a,test4,ghe,"27",test

Output -

b,test,xyz,"23",test
b,test2,abc,"24",test
b,test3,def,"25",test
b,test3,def,"1003",test
b,test3,def,"998",test
b,test3,def,"14356",test
a,test4,ghe,"26",test
a,test4,ghe,"27",test

This way, you look for a number with 5 digits.
Starting with 2, the second digit can be 3, 4 or 5
the third digit can be 1, 0 or 3 and so on
You must use the | (or) to catch what you need.

sed -E '/,"(2[345]|1003|998|14356)",/s/^a,/b,/' infile 
1 Like

Thanks ctac.

Is there any alternative to sed -E as it is saying sed: illegal option --

Am using Sun OS 5.10

Go back to nawk or /usr/xpg4/bin/awk with:

/usr/xpg4/bin/awk -F, '$1 == "a" && $4 ~ /^"(2[3-5]|998|1003|14356)"$/ {$1 = "b"}1' file
1 Like

Thanks Yoda ,ctac and Don for the inputs and helping me. It worked. Thanks again.