Bash string replacement - how to use regex?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash string replacement - how to use regex?
# 1  
Old 01-13-2010
Bash string replacement - how to use regex?

Hello

I have a bash script where I need to do a substring replacement like this:
variable2=${variable1/foo/bar}

However, I only want "foo" replaced if it is at the end of the line.

However, this does not work:
variable2=${variable1/foo$/bar}

as you can see I'm using the $ regex for end of line.

Does anyone know what the correct syntax is?

Thanks
# 2  
Old 01-13-2010
Hi, try:
Code:
variable2="${variable1%foo}bar"

This is not regex by the way....
# 3  
Old 01-13-2010
That works.

Many thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed regex backreference replacement

Hello, I want to rename multiple files and catch some points about backreference within sed and regex. Here is a part of my file list. Input: S92A.fa S92B.fa ... S96Z.fa S921.fa S922.fa ... S997.fa Note: The file names are not necessarily continuous from A~Z or 921 ~ 997, as some of the... (3 Replies)
Discussion started by: yifangt
3 Replies

2. Shell Programming and Scripting

Use regex in replacement string in SED

Hi, I need to use the regex in the replacement string in SED command. something like sed -e ' s/\(^\{5\}\).\{150\}\(.*\)$/\10\{30\}1\{30\}A\{60\}B\{30\}\2/' abc which means for all the lines in file abc that starts with 5 characters, I need to replace character 6-151... (6 Replies)
Discussion started by: snowline84
6 Replies

3. Shell Programming and Scripting

String replacement.

Dear Friends, I want to replace following line with given line. It should grep/search following string in a file (input.txt) M/M SRNO: 000M/6200-0362498 COSMETIC PRO MALE FEMALE Once found it should replace it to following string. T_DLHNNO: 000M/6200-0362498 ... (7 Replies)
Discussion started by: anushree.a
7 Replies

4. Shell Programming and Scripting

filtering out duplicate substrings, regex string from a string

My input contains a single word lines. From each line data.txt prjtestBlaBlatestBlaBla prjthisBlaBlathisBlaBla prjthatBlaBladpthatBlaBla prjgoodBlaBladpgoodBlaBla prjgood1BlaBla123dpgood1BlaBla123 Desired output --> data_out.txt prjtestBlaBla prjthisBlaBla... (8 Replies)
Discussion started by: kchinnam
8 Replies

5. Shell Programming and Scripting

String replacement

Hi All, I have below file which has data in below format. #$ | AB_100 | AB_300 ()| AB_4 @*(% | AB-789 i want o/p as below format. | AB_100 | AB_300 | AB_4 | AB-789 So here there is no standard format. How we can achieve the same in unix ? Regards, (3 Replies)
Discussion started by: gander_ss
3 Replies

6. Shell Programming and Scripting

Bash replacement to getchar

There's a replacement in bash for getchar or get functions of C and C++?Those functions read the next char avaliable in the input stream. I've tried something like: OLD_STTY=`stty -g` stty cbreak -echo look=`dd if=/dev/tty bs=1 count=1 2>/dev/null` stty $OLD_STTY But it is not working... (3 Replies)
Discussion started by: Asafe
3 Replies

7. Shell Programming and Scripting

String replacement

I have one string string1=user/password:IP_ADDR:Directory I need to replace string1 value like store into string2 string2=user password:IP_ADDR:Directory i.e replace "/" character by '<space>' character But i wouldn't use any file in the meantime. Please help me......................... (6 Replies)
Discussion started by: mnmonu
6 Replies

8. Shell Programming and Scripting

String replacement

Hi I am new to shell scripting but i manage to do some simple things. I am trying to replace a string in one file. I am using sed to replace but it is not permanently writing to the file, rather it is temporary. I want to know whether is there any another method to replace a string in a file... (7 Replies)
Discussion started by: reddybs
7 Replies

9. Shell Programming and Scripting

Replacement of string

Hi I have a text file which contains the following. AAA,BBB,CCC,DDD AAA,BBB,CCC,DDD AAA,BBB,CCC,DDD How can I replace all CCC with 888, with other contents inside the file remain unchange? Please advice Desired output: AAA,BBB,888,DDD AAA,BBB,888,DDD AAA,BBB,888,DDD (1 Reply)
Discussion started by: c0384
1 Replies

10. Shell Programming and Scripting

sed problem - replacement string should be same length as matching string.

Hi guys, I hope you can help me with my problem. I have a text file that contains lines like this: 78 ANGELO -809.05 79 ANGELO2 -5,000.06 I need to find all occurences of amounts that are negative and replace them with x's 78 ANGELO xxxxxxx 79... (4 Replies)
Discussion started by: amangeles
4 Replies
Login or Register to Ask a Question