Home Man
Search
Today's Posts
Register

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Replacing field based on the value of other field

Tags
awk, shell scripts, solved

Login to Reply

 
Thread Tools Search this Thread
# 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 02:24 PM..
# 2  
Old 01-12-2018
Using nawk:-
Code:
nawk -F, '$4>=23&&$4<=25{$1="b"}1' OFS=, file

The Following User Says Thank You to Yoda For This Useful Post:
weknowd (01-12-2018)
# 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 02:24 PM..
# 7  
Old 01-12-2018
Please use code tags for code. Select the text then hit the button.
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk to adjust coordinates in field based on sequential numbers in another field cmccabe Shell Programming and Scripting 3 01-30-2017 07:39 AM
awk to update value in field based on another field cmccabe Shell Programming and Scripting 2 12-03-2016 10:52 AM
replacing a value in a field gartie Shell Programming and Scripting 1 06-06-2012 11:01 AM
Replacing the last field maitree Shell Programming and Scripting 6 09-23-2011 10:27 AM
Help with Awk finding and replacing a field based on a condition jghi123 Shell Programming and Scripting 2 05-04-2011 02:22 PM
awk - Summing a field based on another field treesloth UNIX for Dummies Questions & Answers 7 06-21-2009 06:04 PM
Find top N values for field X based on field Y's value FrancoisCN Shell Programming and Scripting 1 05-29-2009 09:57 AM
need help with replacing a certain field... shennanigan83 Shell Programming and Scripting 5 04-09-2008 01:00 AM
Replacing certain field charbel Shell Programming and Scripting 1 04-26-2006 04:00 PM
Replacing the last field of a line. Darek Shell Programming and Scripting 3 08-19-2005 11:45 AM


All times are GMT -4. The time now is 06:25 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password