sed substitution depending on trailing markers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed substitution depending on trailing markers
# 1  
Old 04-04-2017
sed substitution depending on trailing markers

I have a pattern Preceding a # hash in a string which needs to be substituted. However, the substitution value will depend on the content of the last few characters, (_ABC or _RW) of whole stream. [see example below.] I was thinking sed should be used but I' haven't been able to figure it out the right combo. Is sed the correct media for this? Anyone have any ideas?

Code:
DSCAPP123#XYZ_A0015_ABC

The above string would substitute PRDAPP123 for DSCAPP123
while
Code:
DSCAPP123#XYZ_BC00034_RW

This string would substitute PDDAPP123 for DSCAPP123
# 2  
Old 04-04-2017
Code:
$
$ cat data.txt
DSCAPP123#XYZ_A0015_ABC
DSCAPP123#XYZ_BC00034_RW
$
$ sed -e 's/DSCAPP123\(.*_ABC\)/PRDAPP123\1/' -e 's/DSCAPP123\(.*_RW\)/PDDAPP123\1/' data.txt
PRDAPP123#XYZ_A0015_ABC
PDDAPP123#XYZ_BC00034_RW
$
$

# 3  
Old 04-04-2017
Or
Code:
sed '/_ABC$/ s/^DSC/PRD/; /_RW$/ s/^DSC/PDD/' file
PRDAPP123#XYZ_A0015_ABC
PDDAPP123#XYZ_BC00034_RW

These 2 Users Gave Thanks to RudiC For This Post:
# 4  
Old 04-04-2017
Or:
Code:
sed '/_ABC$/s/^DSCAPP123#/PRDAPP123#/; /_RW$/s/^DSCAPP123#/PDDAPP123#/'

These 2 Users Gave Thanks to Scrutinizer For This Post:
# 5  
Old 04-05-2017
sed substitution depending on trailing markers

Thanks for all your help, our version of sed would not accept the syntax suggested by durden_tyler but RudiC's and Scrutinizer examples solved the issue. Again thanks for your prompt replies.

---------- Post updated at 06:45 AM ---------- Previous update was at 06:43 AM ----------

Solved Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed substitution

Hi everyone, I need very simple sed command to change a parameter in a text file. I have a line in this text which is like set xx 0.5 A program reads this file and does some algebraic calculations. So to make a parameter scan I need to change the value of xx. I thought I can do... (7 Replies)
Discussion started by: hayreter
7 Replies

2. UNIX for Dummies Questions & Answers

sed substitution

How can you use sed with a line of code that reads: 67899:Bill:Williams:Maple Dr.:45908600 Let us say we want to replace Maple Dr. with Oak St. (1 Reply)
Discussion started by: yonkers062986
1 Replies

3. Shell Programming and Scripting

Extract data between two markers

Data in my jsp file is as follows: Extract the name of all include page jsp file name <!--- Global Nav Start --> <jsp:include page="/includes/GlobalNav.jsp" > <jsp:param name="CMCat" value="TechProp" /> </jsp:include> <!--- Global Nav End --> <!-- Primary Nav Start --> <jsp:include... (4 Replies)
Discussion started by: rajkdutta
4 Replies

4. Shell Programming and Scripting

How to delete ending/trailing spaces using awk,sed,perl?

How to delete ending/trailing spaces using awk,sed,perl? Input:(each line has extra spaces at the end) 3456 565 3 7 35 878 Expected output: 3456 565 3 7 35 878 (5 Replies)
Discussion started by: cola
5 Replies

5. Shell Programming and Scripting

Removing trailing zeros using sed

Hello All, I have a csv file with 3 columns. The file which looks like this 47850000,100,233 23560000,10000,456 78650000,560000,54 34000000,3456,3 The first column has 4 trailing zeros. I have to remove 4 trailing zeroes from 1st field. The output file should appear as follows. ... (12 Replies)
Discussion started by: grajp002
12 Replies

6. UNIX for Dummies Questions & Answers

Help with sed substitution

I'm a noob to unix, and I have a line of data like the following: title=Boston|tcolor=green|desc=Large city in New England|url=www.boston.com Is there a way to change a field value with sed substitution? (i.e. change tcolor=green to tcolor=blue) I figured out: sed... (19 Replies)
Discussion started by: stabby
19 Replies

7. Shell Programming and Scripting

sed - how to remove trailing slashes

I know you can remove trialing slashes using: #echo "/tmp/one/two/three////" | sed "s,/$,," /tmp/one/two/three/// But I want to know how to make it remove all trialing flashes in the front, and in the start, so the end result is: tmp/one/two/three Anyone have any idea how to do this... (6 Replies)
Discussion started by: EXT3FSCK
6 Replies

8. Shell Programming and Scripting

sed substitution

Hi I am trying to do a text insertion in a text file at a particular line number in a shell script. However its not working. sed '122i\ > for j in \`echo $MyList\` ; do perl -pi -e\'s#01\/01\/2009#01\/01\/2011#\' $j ; done' $HOME/MyScript.ksh The Actual line to be inserted at line 122... (5 Replies)
Discussion started by: som.nitk
5 Replies

9. Shell Programming and Scripting

sed: removing any and all trailing digits?

We have a large number of oracle database related scripts that utilize the environment variables $ORACLE_SID and $DBNAME. In a single instance database the $ORACLE_SID is the same as the database name $DBNAME. So we have simply set DBNAME = $ORACLE_SID. However, now that we are clustering with RAC,... (5 Replies)
Discussion started by: Squeakygoose
5 Replies

10. UNIX for Dummies Questions & Answers

End of record markers

I have a file with thousands of 80 character records. Unfortunately, there are no end of record characters, so any normal script commands that I've tried will process the entire file as one record. I.E. If I use grep to find the record that contains some specific value, I either get nothing... (3 Replies)
Discussion started by: Dave Miller
3 Replies
Login or Register to Ask a Question