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
# 8  
Old 01-12-2018
The requirement change ?
Just add " around 2[345]
# 9  
Old 01-12-2018
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!
# 10  
Old 01-12-2018
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
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..
# 12  
Old 01-12-2018
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)
# 13  
Old 01-12-2018
Thanks ctac.

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

Am using Sun OS 5.10
# 14  
Old 01-12-2018
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)
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 07:02 PM.

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