👤


Shell Programming and Scripting

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

awk, solved

👤 Login to reply
 
Thread Tools Search this Thread Display Modes
    #1  
Old 01-12-2018
weknowd weknowd is offline
Registered User
 
Join Date: Jan 2010
Last Activity: 5 February 2018, 12:58 PM EST
Posts: 52
Thanks: 13
Thanked 0 Times in 0 Posts
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..
Sponsored Links
    #2  
Old 01-12-2018
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 17 July 2018, 3:55 PM EDT
Location: Galactic Empire
Posts: 3,671
Thanks: 257
Thanked 1,331 Times in 1,247 Posts
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)
Sponsored Links
    #3  
Old 01-12-2018
weknowd weknowd is offline
Registered User
 
Join Date: Jan 2010
Last Activity: 5 February 2018, 12:58 PM EST
Posts: 52
Thanks: 13
Thanked 0 Times in 0 Posts
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
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 17 July 2018, 3:55 PM EDT
Location: Galactic Empire
Posts: 3,671
Thanks: 257
Thanked 1,331 Times in 1,247 Posts
Code:
nawk -F, '
        {
                for ( i = 1; i <= NF; i++ )
                {
                        if ( $i >= 23 && $i <= 25 )
                        {
                                $1 = "b"
                        }
                }
        }
        1
' OFS=, file

Sponsored Links
    #5  
Old 01-12-2018
ctac_ ctac_ is offline
Registered User
 
Join Date: Aug 2017
Last Activity: 26 June 2018, 2:58 PM EDT
Posts: 23
Thanks: 3
Thanked 7 Times in 7 Posts
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
Sponsored Links
    #6  
Old 01-12-2018
weknowd weknowd is offline
Registered User
 
Join Date: Jan 2010
Last Activity: 5 February 2018, 12:58 PM EST
Posts: 52
Thanks: 13
Thanked 0 Times in 0 Posts
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..
Sponsored Links
    #7  
Old 01-12-2018
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 17 July 2018, 11:41 AM EDT
Location: Saskatchewan
Posts: 22,724
Thanks: 1,193
Thanked 4,350 Times in 4,005 Posts
Please use code tags for code. Select the text then hit the button.
Sponsored Links
👤 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 based on first 5 filed of column shanul karim UNIX for Beginners Questions & Answers 4 11-22-2017 02:29 AM
Replacing first field of file2 with the second filed of file1 for matching cases ailnilanjan Shell Programming and Scripting 6 11-06-2014 05:14 AM
removing a particular field from some of the lines in a filed dev.devil.1983 Shell Programming and Scripting 2 10-18-2012 04:56 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
replacing one filed at run time in UNix rawatds Shell Programming and Scripting 7 05-11-2006 01:07 AM



All times are GMT -4. The time now is 02:52 AM.

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





Not a Forum Member?
Forgot Password?