Unix/Linux Go Back    


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

Shell Programming and Scripting


Tags
awk, solved

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 1 Week Ago   -   Original Discussion by weknowd
weknowd's Unix or Linux Image
weknowd weknowd is offline
Registered User
 
Join Date: Jan 2010
Last Activity: 22 January 2018, 4:05 PM EST
Posts: 50
Thanks: 12
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; 1 Week Ago at 03:24 PM..
Sponsored Links
    #2  
Old Unix and Linux 1 Week Ago   -   Original Discussion by weknowd
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 23 January 2018, 4:48 PM EST
Location: Galactic Empire
Posts: 3,642
Thanks: 257
Thanked 1,323 Times in 1,240 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 (1 Week Ago)
Sponsored Links
    #3  
Old Unix and Linux 1 Week Ago   -   Original Discussion by weknowd
weknowd's Unix or Linux Image
weknowd weknowd is offline
Registered User
 
Join Date: Jan 2010
Last Activity: 22 January 2018, 4:05 PM EST
Posts: 50
Thanks: 12
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 Unix and Linux 1 Week Ago   -   Original Discussion by weknowd
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 23 January 2018, 4:48 PM EST
Location: Galactic Empire
Posts: 3,642
Thanks: 257
Thanked 1,323 Times in 1,240 Posts


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

Sponsored Links
    #5  
Old Unix and Linux 1 Week Ago   -   Original Discussion by weknowd
ctac_'s Unix or Linux Image
ctac_ ctac_ is offline
Registered User
 
Join Date: Aug 2017
Last Activity: 12 January 2018, 4:42 PM EST
Posts: 15
Thanks: 2
Thanked 3 Times in 3 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 Unix and Linux 1 Week Ago   -   Original Discussion by weknowd
weknowd's Unix or Linux Image
weknowd weknowd is offline
Registered User
 
Join Date: Jan 2010
Last Activity: 22 January 2018, 4:05 PM EST
Posts: 50
Thanks: 12
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; 1 Week Ago at 03:24 PM..
Sponsored Links
    #7  
Old Unix and Linux 1 Week Ago   -   Original Discussion by weknowd
Corona688's Unix or Linux Image
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 23 January 2018, 2:54 PM EST
Location: Saskatchewan
Posts: 22,574
Thanks: 1,164
Thanked 4,293 Times in 3,961 Posts
Please use code tags for code. Select the text then hit the Linux button.
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 03: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 06: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 05:56 AM
Help with Awk finding and replacing a field based on a condition jghi123 Shell Programming and Scripting 2 05-04-2011 03:22 PM
replacing one filed at run time in UNix rawatds Shell Programming and Scripting 7 05-11-2006 02:07 AM



All times are GMT -4. The time now is 05:48 PM.