Bash string replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash string replace
# 8  
Old 11-22-2009
Code:
# STRING="Hello World! word1 word2 AND another word3 at the END.";
# FILTERED=${STRING// word[1-9]/};
# echo "$FILTERED"
Hello World! AND another at the END.

Works on bash
# 9  
Old 11-22-2009
Ok thanks.

Code:
str="It's cold, very cold"
pattern=${str/cold/hot}
echo $pattern

Is possible to replace the second "cold" by "hot" and keep the first "cold" with extended globbing ?
# 10  
Old 11-23-2009
Code:
echo ${str%cold}hot

But I suspect that does not satisfy your question Smilie
# 11  
Old 11-23-2009
Quote:
Originally Posted by Scrutinizer
Code:
echo ${str%cold}hot

But I suspect that does not satisfy your question Smilie
It do, thanks.
The % char is used to read from the end of str ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search partial string in a file and replace the string - UNIX

I have the below string which i need to compare with a file and replace this string in the file which matches closely. Can anyone help me on this. string(Scenario 1)- user::r--,user::ourfrd:r-- String(Scenario 2)- user::r-- File **** # file: /local/Desktop/myfile # owner: me # group:... (6 Replies)
Discussion started by: sarathy_a35
6 Replies

2. Shell Programming and Scripting

[bash] - Replace blank and string in csv file

Hi all, i have a .csv file with only two columns, like: Login;Status Luca;S Marco; Stefano; Elettra;S Laura; ... I need to replace the blank space on Status column whit Enabled end, on the same column, S whit Disabled, like: Login;Status Luca;Disabled Marco;Enabled Stefano;Enabled... (10 Replies)
Discussion started by: kamose
10 Replies

3. UNIX for Advanced & Expert Users

BASH Internal : Replace pattern with string without external command

Morning, I'm trying step up my scripting game .. :rolleyes::confused::D Is there a way to do the replacement with an or without using an external command ? I did try but no joy. var=${var//\(|\)/} #!/bin/bash var="lulus.UbiRwidgets.com (10.1.1.1)" var=${var//\(/}... (5 Replies)
Discussion started by: popeye
5 Replies

4. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

5. UNIX for Dummies Questions & Answers

Replace a String using Bash

I'm going freakin crazy here! I've tried multiple attempts and configurationa and cannot get this to work. I have a file: private/etc/apt/sources.list.d/cydia.list I want to replace a string in this file: "deb http:name.of.address ./" with "deb http:name.of.other.address ./" The... (4 Replies)
Discussion started by: thazsar
4 Replies

6. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

7. Shell Programming and Scripting

Requesting help to replace a string by my bash script

Hello every1, I need help to replace a string in a file by my bash script. Find: log4j.appender.toLogFile.layout.ConversionPattern= %d %5p (%F:%L) - %m%n= %d %5p (%F:%L) - %m%n Replace: log4j.appender.toLogFile.layout.ConversionPattern= %d %5p (%F:%L) - %m%n I tried by sed, but kept... (7 Replies)
Discussion started by: titanic4u
7 Replies

8. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies

9. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

10. Shell Programming and Scripting

Search, replace string in file1 with string from (lookup table) file2?

Hello: I have another question. Please consider the following two sample, tab-delimited files: File_1: Abf1 YKL112w Abf1 YAL054c Abf1 YGL234w Ace2 YKL150w Ace2 YNL328c Cup9 YDR441c Cup9 YDR442w Cup9 YEL040w ... File 2: ... ABF1 YKL112W ACE2 YLR131C (9 Replies)
Discussion started by: gstuart
9 Replies
Login or Register to Ask a Question