LinuxQuestions.org
Help answer threads with 0 replies.
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 01-02-2018, 10:25 AM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Rep: Reputation: Disabled
Sed , search exact and replace with a variable


Happy new year to everyone .
I have to be honest , you guys will read much more questions with sed doubts i have .
SED for me is the most complicated tool ever existed in linux , the author could build sed less complicated , but like in everything , they tend to complicate everything .

I have a doubt in searching an exact string in a text and replace it with a variable .

I need to search for an exact string and replace it with a variable

some text with anything there and the word "var1" that i want to replace
Code:
a1="whatever"
sed "s/\<var1\>/$a1/g" < inputfile > outputfile
eventually , this sed code does not work with the variable as the output string to be written .

How to solve it ?

Last edited by pedropt; 01-02-2018 at 01:03 PM.
 
Old 01-02-2018, 10:37 AM   #2
rhubarbdog
Member
 
Registered: Apr 2015
Location: Yorkshire, England
Distribution: Linux Mint
Posts: 145

Rep: Reputation: Disabled
Code:
sed "s/\(.*\)var1\(.*\)/\1$a1\2/"

Last edited by rhubarbdog; 01-02-2018 at 10:38 AM.
 
Old 01-02-2018, 11:17 AM   #3
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
With this InFile ...
Code:
Vanilla chocolate
apple var1 peach
Honda var1 Toyota var1 Nissan
Volvo BMW var1 Mercedes
var1
var1 red var1 blue var1
health and happiness
... this sed ...
Code:
sed 's/var1/'$a1'/g' $InFile >$OutFile
... produced this OutFile ...
Code:
Vanilla chocolate
apple whatever peach
Honda whatever Toyota whatever Nissan
Volvo BMW whatever Mercedes
whatever
whatever red whatever blue whatever
health and happiness
Daniel B. Martin

.
 
Old 01-02-2018, 12:00 PM   #4
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
It does not work in my code both of examples .

here it is a quick script to test.

test
Code:
#!/bin/bash
a1="/dev/sda1"
sed "s/var2/'$a1'/g" data > temp1
create a file on same directory with name "data" and put this line inside :
Quote:
<p class="style2">HDD Device : var2</p>
then test the script


i get this message :
sed: -e expression #1, char 10: unknown option to `s'

Last edited by pedropt; 01-02-2018 at 12:02 PM.
 
Old 01-02-2018, 12:23 PM   #5
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
Quote:
Originally Posted by pedropt View Post
i get this message :
sed: -e expression #1, char 10: unknown option to `s'
Works properly on my machine.

Daniel B. Martin

.
 
Old 01-02-2018, 12:53 PM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,792

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
The shell substitutes the variable to
Code:
sed "s/var2/'/dev/sda1'/g" data
And sed sees too many /////
Take another delimiter!
Code:
a1="/dev/sda1"
sed "s|var2|'$a1'|g" data
 
2 members found this post helpful.
Old 01-02-2018, 01:03 PM   #7
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
Thanks "MadeInGermany" , it works perfectly
 
  


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
(SOLVED) Search and Replace Exact string without removing the line pedropt Programming 6 09-10-2017 03:53 AM
[Solved] Search & Replace exact string with sed pedropt Programming 17 06-25-2017 02:57 PM
[SOLVED] how replace exact word using sed santhosh.ananthula Linux - Desktop 4 01-27-2016 05:10 PM
[SOLVED] search and replace particular word with sed using variable roopakl Linux - Newbie 2 10-01-2012 11:08 AM
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 03:44 PM

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

All times are GMT -5. The time now is 03:31 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