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
    #8  
Old Unix and Linux 01-12-2018   -   Original Discussion by weknowd
ctac_'s Unix or Linux Image
ctac_ ctac_ is offline
Registered User
 
Join Date: Aug 2017
Last Activity: 21 March 2018, 1:33 PM EDT
Posts: 21
Thanks: 2
Thanked 6 Times in 6 Posts
The requirement change ?Linux
Just add " around 2[345]
Sponsored Links
    #9  
Old Unix and Linux 01-12-2018   -   Original Discussion by weknowd
weknowd's Unix or Linux Image
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 ctac. It works.



Code:
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

Last edited by MadeInGermany; 01-12-2018 at 03:50 PM.. Reason: code tags!
Sponsored Links
    #10  
Old Unix and Linux 01-12-2018   -   Original Discussion by weknowd
ctac_'s Unix or Linux Image
ctac_ ctac_ is offline
Registered User
 
Join Date: Aug 2017
Last Activity: 21 March 2018, 1:33 PM EDT
Posts: 21
Thanks: 2
Thanked 6 Times in 6 Posts
Yes we can.
But give a representative input and expected output.
And give also what you have try to resolve your problem.
    #11  
Old Unix and Linux 01-12-2018   -   Original Discussion by weknowd
weknowd's Unix or Linux Image
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 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


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

Input -



Code:
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 -



Code:
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

Moderator's Comments:
Replacing field based on the value of other field Please use code tags as required by the forum rules!
Select the text then hit the co/de button at the top of the Wiki editor.

Last edited by MadeInGermany; 01-12-2018 at 03:58 PM..
Sponsored Links
    #12  
Old Unix and Linux 01-12-2018   -   Original Discussion by weknowd
ctac_'s Unix or Linux Image
ctac_ ctac_ is offline
Registered User
 
Join Date: Aug 2017
Last Activity: 21 March 2018, 1:33 PM EDT
Posts: 21
Thanks: 2
Thanked 6 Times in 6 Posts
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.


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

The Following User Says Thank You to ctac_ For This Useful Post:
weknowd (01-12-2018)
Sponsored Links
    #13  
Old Unix and Linux 01-12-2018   -   Original Discussion by weknowd
weknowd's Unix or Linux Image
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 ctac.

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

Am using Sun OS 5.10
Sponsored Links
    #14  
Old Unix and Linux 01-12-2018   -   Original Discussion by weknowd
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is online now Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 20 April 2018, 12:25 PM EDT
Location: San Jose, CA, USA
Posts: 11,207
Thanks: 633
Thanked 3,903 Times in 3,341 Posts
Go back to nawk or /usr/xpg4/bin/awk with:


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

The Following User Says Thank You to Don Cragun For This Useful Post:
weknowd (01-12-2018)
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 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 12:31 PM.