Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 02-11-2014, 01:34 PM   #1
LQ Newbie
Registered: Feb 2014
Posts: 4

Rep: Reputation: Disabled
NEED to replace some text

I need to replace the occurrence of "badfile 'C:users\username\desktop" with "badfile 'filepath\badfile" on FileA. In other words, replace badfile 'C:users\username\desktop with badfile 'filepath\badfile

I tried the following but no effect

test=badfile 'C:users\\username\\desktop
test1=badfile 'filepath\\badfile

sed -i 's|$test|$test1|g' FileA

I need help on how to make this code work or a better approach will be appreciated. Oh..I'm a newbie

Last edited by victordcweb; 02-11-2014 at 01:37 PM.
Old 02-11-2014, 01:47 PM   #2
LQ Guru
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,051

Rep: Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852
i would use awk:
awk '{print $2 "\\" $1}' victordcweb.txt

Last edited by schneidz; 02-11-2014 at 01:48 PM.
Old 02-11-2014, 01:48 PM   #3
Senior Member
Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,845
Blog Entries: 36

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Welcome to LQ victor,
Please use code bbtags to wrap your code. That being said your test is failing because you're using single quotes.

sed -i 's|$test|$test1|g' FileA
is being interpreted by bash literally. If you want bash to expand those variables you'll need to encase in double quotes or exclude the variables.
sed -i "s|$test|$test1|g" FileA
sed -i 's|'"$test"'|'"$test1"'|g' FileA
In my opinion the easier solution would be to just use vim and do the replacement there, :%s/search/replace/g

Last edited by sag47; 02-11-2014 at 01:50 PM.
Old 02-11-2014, 03:33 PM   #4
LQ Newbie
Registered: Feb 2014
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thanks for the reply. The solution below solves the question I posted earlier

test="badfile 'C:users\\\\username\\\\desktop"
test1="badfile 'filepath\\\\badfile"

echo test
echo test1

sed -i "s|$test|$test1|g" FileA
Old 02-11-2014, 05:12 PM   #5
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,247

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Glad you've got a soln.
As per sag47 though, when posting code/data please use CODE tags as described here
This makes it easier to read and preserves the formatting eg whitespace etc.


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] How to replace text using sed or awk in a file without changing the commented text coolpraz Programming 4 01-03-2013 07:19 AM
using sed to replace text on one line in a text file vo1pwf Linux - Newbie 5 06-24-2009 08:54 AM
bash script to create text in a file or replace value of text if already exists knightto Linux - Newbie 5 09-11-2008 12:13 AM
Replace text of unknown content with other text in file brian0918 Programming 15 07-14-2005 10:22 PM
Replace text of unknown content with other text in file brian0918 Linux - Software 1 07-14-2005 04:22 PM

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

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