LinuxQuestions.org
Review your favorite Linux distribution.
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, 11:25 AM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 194

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 02:03 PM.
 
Old 01-02-2018, 11:37 AM   #2
rhubarbdog
Member
 
Registered: Apr 2015
Location: Yorkshire, England
Distribution: Linux Mint
Posts: 134

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

Last edited by rhubarbdog; 01-02-2018 at 11:38 AM.
 
Old 01-02-2018, 12:17 PM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,695

Rep: Reputation: 561Reputation: 561Reputation: 561Reputation: 561Reputation: 561Reputation: 561
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, 01:00 PM   #4
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 194

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 01:02 PM.
 
Old 01-02-2018, 01:23 PM   #5
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,695

Rep: Reputation: 561Reputation: 561Reputation: 561Reputation: 561Reputation: 561Reputation: 561
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, 01:53 PM   #6
MadeInGermany
Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 934

Rep: Reputation: 403Reputation: 403Reputation: 403Reputation: 403Reputation: 403
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, 02:03 PM   #7
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 194

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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

All times are GMT -5. The time now is 03:50 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration