BASH Internal : Replace pattern with string without external command


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users BASH Internal : Replace pattern with string without external command
# 1  
Old 02-02-2015
Hammer & Screwdriver BASH Internal : Replace pattern with string without external command

Morning,
I'm trying step up my scripting game .. SmilieSmilieSmilie

Is there a way to do the replacement with an or without using an external command ?

I did try but no joy.

Code:
var=${var//\(|\)/}



Code:
#!/bin/bash
var="lulus.UbiRwidgets.com (10.1.1.1)"
var=${var//\(/}
var=${var//\)/}
echo "$var"

Thanks !!
Have a great day !!
Pop
# 2  
Old 02-02-2015
Do what replacement?

Code which doesn't do what you want doesn't explain what you do want.
# 3  
Old 02-02-2015
Come on, Corona! Having telephatic abilities is part of the job description, no? ;-))

Being the better psychic i conclude you want to extract the IP-address of your variables content, i.e. the part between "(" and ")".

Carefully reread the part of the man page dealing with "${var%%...}" and "${var##...}" - the one cuts off from the beginning of the variable, the other from the end. You basically do your conversion in two steps, first everything up to "(" then everything behind ")".

I won't spoil the fun finding out how it works, therefore the exact implementation is left as an exercise to the reader.

I hope this helps.

bakunin
# 4  
Old 02-02-2015
# 5  
Old 02-02-2015
Sorry fellas,

Being in the expert bucket I figured you guys would "see" it. ... and if you execute the script I provided you will see that it actually works and the desired output is shown. No mind reading necessary !

I am trying to remove the "(" and the ")" from var without using an external command in a single step.
# 6  
Old 02-02-2015
You posted that it didn't work. I took your word for it.

Code:
echo "${VAR//[()]/}"

These 2 Users Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace String matching wildcard pattern

Hi, I know how to replace a string with another in a file. But, i wish to replace the below string pattern EncryptedPassword="{gafgfa}]\asffafsf312a" i.e EncryptedPassword="<any random string>" To EncryptedPassword="" i.e remove the random password to a empty string. Can you... (3 Replies)
Discussion started by: mohtashims
3 Replies

2. Shell Programming and Scripting

sed command to replace two character pattern with another pattern

Not able to paste my content. Please see the attachment :-( (2 Replies)
Discussion started by: vivek d r
2 Replies

3. Shell Programming and Scripting

Replace string in line below specific pattern?

Hi, I'm trying to replace a string with sed, in a text file containing this pattern: location alpha value x location beta value y location gamma value y location delta value y location theta value z ... What I want to achieve is: Find location beta into text file... (1 Reply)
Discussion started by: TECK
1 Replies

4. Red Hat

Disk in Linux machine are internal or external

How to check whether the disk in linux machine are internal or external ( from nas or san). How to identify internal(local) and external disks. Following are some details of my server. Thanks. #df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 15G 3.5G 10G... (2 Replies)
Discussion started by: salmanraza
2 Replies

5. Shell Programming and Scripting

Replace a string pattern

Hi, I have a CSV with following type of data and would like to replace the timestamp information with 'null' string. Can you please suggest me on same? 8,1,'1','1',11,'2013-08-12 18:34:17.0','null',1,'2013-08-12 18:34:17.0','null','PROMOTIONAL','12','1','11','11',11,'0' Thanks for your... (10 Replies)
Discussion started by: bhupinder08
10 Replies

6. Shell Programming and Scripting

lftp script to connect to external sftp site and download to internal ftp and then send email

Hi there, I'm new to shell scripting and need some help if possible? I need to create a shell script (.sh) to run as a cron job on an ubuntu linux server to connect to an external sftp sites directory using credentials (which I have) and then download to our internal ftp server and then copy... (3 Replies)
Discussion started by: ghath
3 Replies

7. 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

8. Shell Programming and Scripting

using an awk internal variable as parameter for an external array

Hello, I am running a bash script under linux which first defines an CA-array like j=0 num1=120.00 num2=10.00 until do CA='echo $num1 + $j*$num2' j=$ done within the later awk section of this same script I want to read data from a file. If the value of the second column is... (3 Replies)
Discussion started by: MotAah
3 Replies

9. Shell Programming and Scripting

Match pattern and replace with string

hi guys, insert into /*<new>*/abc_db.tbl_name this is should be replaced to insert into /*<new>*/${new}.tbl_name it should use '.' as delimiter and replace is there any way to do it using sed (6 Replies)
Discussion started by: sol_nov
6 Replies
Login or Register to Ask a Question