LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 08-05-2015, 10:28 AM   #1
QWE123
Member
 
Registered: Nov 2014
Posts: 55

Rep: Reputation: Disabled
replace the string


I have a file , the content as below ,

#vi file
Code:
aaa bbb ccc ddd
I would like to remove the string "bbb ccc" , I use the below command
Code:
sed -i 's/bbb ccc//g' file
the output file will be changed to as below
Code:
"aaa     dddd"
it seems works but there are space between "aaa" and "ddd" , what I need is cut the character but remove the space , the output should be as below , would advise what can I do ? thanks
Code:
"aaa ddd"

Last edited by QWE123; 08-05-2015 at 10:31 AM.
 
Old 08-05-2015, 11:17 AM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,773
Blog Entries: 13

Rep: Reputation: 4818Reputation: 4818Reputation: 4818Reputation: 4818Reputation: 4818Reputation: 4818Reputation: 4818Reputation: 4818Reputation: 4818Reputation: 4818Reputation: 4818
That's confusing. You're illustration shows a lot of spaces between the aaa and ddd, and adds a d to the term. And then you say it should show just a single space.

If you know you have "term SPACE term SPACE term SPACE term" then you account for those spaces in your sed search and replace term. You should use single quotes to surround the entire term and delimit your spaces with backslash.

Try:
Code:
sed -i 's/'\ bbb\ ccc\ '//g' <file>
and you should get just "aaaddd" and then it's a matter of what spaces you choose to delete versus not in that first term.

You can or can not use the quotes around the entire sed term:
Code:
sed -i s/'\ bbb\ ccc\ '//g <file>

Last edited by rtmistler; 08-06-2015 at 07:03 AM.
 
Old 08-05-2015, 07:00 PM   #3
QWE123
Member
 
Registered: Nov 2014
Posts: 55

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by rtmistler View Post
That's confusing. You're illustration shows a lot of spaces between the aaa and ddd, and adds a d to the term. And then you say it should show just a single space.

If you know you have "term SPACE term SPACE term SPACE term" then you account for those spaces in your sed search and replace term. You should use single quotes to surround the entire term and delimit your spaces with backslash.

Try:
Code:
sed -i 's/'\ bbb\ ccc\ //g' <file>
and you should get just "aaaddd" and then it's a matter of what spaces you choose to delete versus not in that first term.

You can or can not use the quotes around the entire sed term:
Code:
sed -i s/'\ bbb\ ccc\ '//g <file>
thanks reply ,

your first example can not be run , it seems missing a quote , would advise what should be the command ? thank
Code:
sed -i 's/'\ bbb\ ccc\ //g' <file>
 
Old 08-06-2015, 07:07 AM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,773
Blog Entries: 13

Rep: Reputation: 4818Reputation: 4818Reputation: 4818Reputation: 4818Reputation: 4818Reputation: 4818Reputation: 4818Reputation: 4818Reputation: 4818Reputation: 4818Reputation: 4818
Sorry, edited my original to put the quote properly in.
 
  


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
How to replace a string if another specific string exists in the line victorcheung Programming 3 06-26-2015 07:20 PM
How to show selected string using grep from file and replace it with new input string prasad1990 Linux - Software 2 03-19-2015 08:02 AM
[SOLVED] replace string with empty string of dir path ted_chou12 Linux - Newbie 3 04-02-2011 06:43 AM
how do i replace a text string in a file with a random string? (with sed etc) steve51184 Linux - Software 16 09-02-2010 11:05 AM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 01:04 AM

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

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