![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
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;
|
|
||||
|
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)/}
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.. |
|
||||
|
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)}
|
|
||||
|
Yes, shopt was missing.
|
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|