×
UNIX.COM Login
Username:
Password:  
Show Password






👤


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
    #8  
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
The requirement change ?
Just add " around 2[345]
Sponsored Links
    #9  
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 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 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
Yes we can.
But give a representative input and expected output.
And give also what you have try to resolve your problem.
    #11  
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 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 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
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 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 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 01-12-2018
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 17 July 2018, 7:22 PM EDT
Location: San Jose, CA, USA
Posts: 11,407
Thanks: 649
Thanked 3,970 Times in 3,393 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
👤 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 09:19 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.