Remove text from String | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Remove text from String

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-24-2011
Coolman24 Coolman24 is offline
Registered User
 
Join Date: Sep 2011
Last Activity: 4 October 2011, 3:30 PM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Remove text from String

Hello All,

I am new to Bash Programming, I just started a few days ago. OK now to my Question. I want to remove a text from a string. Example:


Code:
var="Hello Bad World!"
echo $var
Output: Hello Bad World!

But i want:
Hello World!

How can i remove the word "Bad" ? I heard about sed but how does it work with words?

Thanks!

Last edited by Coolman24; 09-24-2011 at 04:21 PM..
Sponsored Links
    #2  
Old 09-24-2011
anishkumarv anishkumarv is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 14 May 2013, 12:44 AM EDT
Posts: 96
Thanks: 15
Thanked 2 Times in 2 Posts
sed -i 's/Bad//g' file
Sponsored Links
    #3  
Old 09-24-2011
birei birei is offline
Registered User
 
Join Date: Oct 2010
Last Activity: 24 June 2014, 5:36 AM EDT
Location: Bilbao, Spain
Posts: 628
Thanks: 8
Thanked 173 Times in 171 Posts
Hi Coolman24,


Code:
$ var='Hello Bad World!'
$ echo $var
Hello Bad World!
$ echo $var | sed 's/[ ][^ ]*//'
Hello World!

Regards,
Birei
    #4  
Old 09-24-2011
greet_sed greet_sed is offline
Registered User
 
Join Date: Aug 2011
Last Activity: 5 July 2014, 5:50 AM EDT
Posts: 124
Thanks: 20
Thanked 33 Times in 32 Posts
@birei,
do you mind explaining the expression
thanks.
Sponsored Links
    #5  
Old 09-24-2011
Coolman24 Coolman24 is offline
Registered User
 
Join Date: Sep 2011
Last Activity: 4 October 2011, 3:30 PM EDT
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks.

@anishkumarv

That works. But I cant use a String at "file"

@birei

That dont work if the string is "HelloBadWorld!"
Thanks anyway.
Sponsored Links
    #6  
Old 09-24-2011
birei birei is offline
Registered User
 
Join Date: Oct 2010
Last Activity: 24 June 2014, 5:36 AM EDT
Location: Bilbao, Spain
Posts: 628
Thanks: 8
Thanked 173 Times in 171 Posts
@Coolman24

I tried but fine since you already got the correct answer from anishkumarv

@greet_sed

Yes.

[ ] -> Match one space.
[^ ]* -> Match any characters until it finds a space.
s/...// -> Replace with nothing (delete).
Regards,
Birei
The Following User Says Thank You to birei For This Useful Post:
greet_sed (09-24-2011)
Sponsored Links
    #7  
Old 09-24-2011
anishkumarv anishkumarv is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 14 May 2013, 12:44 AM EDT
Posts: 96
Thanks: 15
Thanked 2 Times in 2 Posts
Hi,

the problem is, you can't use sed to replace in a file, you have to use a temporary file for this.


Code:
sed -i 's/Bad//g'  file > new file1

cat file1


Last edited by Franklin52; 09-26-2011 at 08:36 AM.. Reason: Please use code tags for data and code samples, thank you
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
remove characters from string based on occurrence of a string victor369 Shell Programming and Scripting 5 02-03-2011 08:37 PM
Remove duplicate files based on text string? spangberg Shell Programming and Scripting 1 08-28-2009 02:10 AM
remove text from string shantanuo UNIX for Dummies Questions & Answers 7 11-07-2008 12:55 AM
Search a string and append text after the string kesu2k Shell Programming and Scripting 8 07-18-2008 07:35 PM
appending string to text file based on search string malaymaru Shell Programming and Scripting 1 06-09-2006 08:53 AM



All times are GMT -4. The time now is 05:35 PM.