The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
awk - replace number of string length from search and replace for a serialized array otrotipo Shell Programming and Scripting 1 07-10-2009 12:04 PM
Search, replace string in file1 with string from (lookup table) file2? gstuart Shell Programming and Scripting 9 06-08-2009 06:11 AM
Search for a string and replace the searched string in the same position in samefile ganesh_248 UNIX for Dummies Questions & Answers 27 03-23-2009 12:35 AM
Search for a string and replace the searched string in the same position ganesh_248 Shell Programming and Scripting 15 02-09-2009 09:35 PM
Find the position of a string and replace with another string bab123 Shell Programming and Scripting 6 01-21-2009 04:14 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 5 Days Ago
limmer limmer is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 7
Bash string replace

Bash shell. I'm trying to filter a string taken from user input. I can replace one word at a time. This method supports regex, so is it possible to replace various words at a time?
Code:
STRING="Hello World! word1 word2";

FILTERED=${STRING/word1|word2/}; # Not working: replace 2 or more words ???

echo $FILTERED;
  #2 (permalink)  
Old 5 Days Ago
Scrutinizer Scrutinizer is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 584
Parameter expansion uses shell pattern matching, not regex.
You could use e.g. to filter out either or both words and the space before it.
Code:
FILTERED=${STRING//@( word1| word2)/}
You need to use a double slash otherwise it will just match one word in case there are two matching words.

or, if you do not seek to filter out these specific words, just plain:
Code:
FILTERED=${STRING% * *}

Last edited by Scrutinizer; 5 Days Ago at 03:02 PM..
  #3 (permalink)  
Old 5 Days Ago
limmer limmer is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 7
I tried this code but is not functional (at least in bash). FILTERED still outputs the string unchanged. The second code posted does work though.


FILTERED=${STRING//@( word1| word2)/}
  #4 (permalink)  
Old 5 Days Ago
Scrutinizer Scrutinizer is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 584
Does this work?

bash code:
Code:
shopt -s extglob
STRING="Hello World! word1 word2"
FILTERED=${STRING//@( word1| word2)/}
echo $FILTERED


---------- Post updated at 12:55 PM ---------- Previous update was at 12:28 PM ----------

This should work too:
Code:
FILTERED=${STRING/+( word1| word2)}
  #5 (permalink)  
Old 5 Days Ago
limmer limmer is offline
Registered User
  
 

Join Date: Nov 2009
Posts: 7
Yes, shopt was missing.
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:58 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0