LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-06-2024, 12:53 PM   #16
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660

To further generalize...

Suppose the alchemist want to change two base metals to one precious metal.

With this InFile ...
Code:
m1_IRON some other text m2_IRON
p2_LEAD yet more text p2_IRON extra text
change is good hello_LEAD
... this code ...
Code:
BaseMetal1='IRON'
BaseMetal2='LEAD'
PreciousMetal='GOLD'
sed -r 's/([[:alnum:]]+)_('$BaseMetal1'|'$BaseMetal2')/'$PreciousMetal'_\1/g' <$InFile >$OutFile
... produces this OutFile ...
Code:
GOLD_m1 some other text GOLD_m2
GOLD_p2 yet more text GOLD_p2 extra text
change is good GOLD_hello
Daniel B. Martin

.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
check leading and trailing white space from a variable and fail the script dilip_d21 Linux - Software 20 12-13-2021 09:03 AM
LXer: Add leading zeroes that aren't really leading LXer Syndicated Linux News 0 09-13-2019 12:31 PM
LXer: Leading and trailing whitespace LXer Syndicated Linux News 0 06-28-2019 03:04 AM
Truncating trailing characters liguorir Programming 0 05-23-2004 04:00 AM
passwords with trailing characters murray_linux Slackware 4 04-08-2004 11:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:41 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration