LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices


Reply
  Search this Thread
Old 02-11-2014, 01:34 PM   #1
victordcweb
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
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,165

Rep: Reputation: 889Reputation: 889Reputation: 889Reputation: 889Reputation: 889Reputation: 889Reputation: 889
i would use awk:
Code:
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
sag47
Senior Member
 
Registered: Sep 2009
Location: Orange County, CA
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,851
Blog Entries: 36

Rep: Reputation: 455Reputation: 455Reputation: 455Reputation: 455Reputation: 455
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.

Code:
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.
Code:
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
victordcweb
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
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
Glad you've got a soln.
As per sag47 though, when posting code/data please use CODE tags as described here https://www.linuxquestions.org/quest...do=bbcode#code.
This makes it easier to read and preserves the formatting eg whitespace etc.
 
  


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] 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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:13 AM.

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