Replacing whole string starting with specific works


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing whole string starting with specific works
# 1  
Old 12-25-2015
Wrench Replacing whole string starting with specific works

Hi guys,

So what I am trying to accomplish is to replace a whole string starting with some designated string.

eg:

When even I find a string starting with :

Code:
eai.endpoint.url=

replace the entire line with:
Code:
eai.endpoint.url=http://www.endpoint.com/API

Righ now I am trying to accomplish this using sed and regex.


Code:
BASE_FILE=/opt/sh.properties
CAC_ENDPOINT="eai.endpoint.url=*"
NEW_ENDPOINT="eai.endpoint.url=http://www.endpoint.com/API"


sed -i "s/${CAC_ENDPOINT}/${NEW_ENDPOINT}/g" $BASE_FILE

But seems like regex is not working and I am getting:

Code:
sed: -e expression #1, char 45: unknown option to `s'


For me :

Code:
eai.endpoint.url=*

means find all lines starting with eai.endpoint.url=

Am I doing this wrong ?
# 2  
Old 12-25-2015
Hi, the biggest problem is with the slashes that are part of the variables. Try using | as a delimiter instead..:
Code:
sed "\|${CAC_ENDPOINT}|s|.*|${NEW_ENDPOINT}|"

This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 12-25-2015
Quote:
Code:
sed: -e expression #1, char 45: unknown option to `s'

${NEW_ENDPOINT} has / inserted in it and when it gets expanded sed sees
s/eai.endpoint.url=*/eai.endpoint.url=http://www.endpoint.com/API/g
The slashes in the URI gets confused with the delimiters.
Escaping them in the variable assignment might work as intended.
Code:
NEW_ENDPOINT='eai.endpoint.url=http:\/\/www.endpoint.com\/API'

There is a subtle issue as well.
Code:
CAC_ENDPOINT="eai.endpoint.url=*"

The * is a pattern matching for the shell and it gets interpreted by it. This variable might end up having assigned the name of every file in the directory that would match that. Furthermore, if it makes it as just a string, when it gets converted as a regex in sed, it only means match the following chars: eai followed by any char except the newline followed by endpoint(these are individual chars), followed by any character except the newline, followed by url, followed by one or many = or none.
This User Gave Thanks to Aia For This Post:
# 4  
Old 12-25-2015
Quote:
Originally Posted by Junaid Subhani
For me :

Code:
eai.endpoint.url=*

means find all lines starting with eai.endpoint.url=
As it is your regex means something different: First, "." means "any single character", and "*" means "the preceding expression or single character is optional". Your regex therefore means:

"eai", followed by any single character, followed by the fixed string "endpoint", followed by any single character, followed by "url" and optionally a "=" at the end. This string, for instance: "eaiXendpointYurl" would match your regex.

This here would do what you want (note the difference between "." - any character - and "\." - a literal dot. Further, as you said the string you search for is at the beginning of the line, i added a "^" as anchor. It symbolizes the beginning of line):

Code:
^eai\.endpoint\.url=.*

It is probably easier to write your replacement like this (you won't need the final ".*"):

Code:
sed '/^eai\.endpoint\.url=/ s|.*|eai.endpoint.url=http://www.endpoint.com/API|'

or, using the variable names you used - note the quoting:

Code:
CAC_ENDPOINT='^eai\.endpoint\.url='
NEW_ENDPOINT='eai.endpoint.url=http://www.endpoint.com/API'

sed '/'"{CAC_ENDPOINT}"'/ s|.*|'"${NEW_ENDPOINT}"'|'

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 5  
Old 12-25-2015
Thanks guys for the answers and their explanations. Worked perfectly !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep a string and count following lines starting with another string

I have a large dataset with following structure; C 0001 Carbon D SAR001 methane D SAR002 ethane D SAR003 propane D SAR004 butane D SAR005 pentane C 0002 Hydrogen C 0003 Nitrogen C 0004 Oxygen D SAR011 ozone D SAR012 super oxide C 0005 Sulphur D SAR013... (3 Replies)
Discussion started by: Syeda Sumayya
3 Replies

2. Shell Programming and Scripting

Extract specific line in an html file starting and ending with specific pattern to a text file

Hi This is my first post and I'm just a beginner. So please be nice to me. I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file? I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 Replies

3. Shell Programming and Scripting

Matching and Replacing file lines starting with $

Here is the task that I was presented with: I am dealing with about a 10,000 line input deck file for an analysis. About 10 separate blocks of around 25 lines of code each need to be updated in the input deck. The input deck (deckToChange in the code below) comes with 2 separate files. File 1... (5 Replies)
Discussion started by: tiktak292
5 Replies

4. Shell Programming and Scripting

How to delete lines starting with specific string?

Dear all, I would like to delete even lines starting with "N" together with their respective titles which are actually odd lines. Below is the example of input file. I would like to remove line 8 and 12 together with its title line, i.e., line 7 and 11, respectively.... (2 Replies)
Discussion started by: huiyee1
2 Replies

5. Shell Programming and Scripting

how to find entries, NOT starting with specific pattern

Hey,I have a file in following format >1 ABC........ >2 XYZ..... >3 ABC........ >4 MNO....... >5 ABC....... now I would like to find only those entries that doesn't start with ABC (specific pattern)e.g preferred output: >2 XYZ.... >4 MNO....... it will be nice if anybody how... (2 Replies)
Discussion started by: ankitachaurasia
2 Replies

6. Shell Programming and Scripting

Grep starting from a specific position

Hello people, I'm scratch my head to find a solution to my problem, I'm absolutely sure this is very simple!!! :wall: I'm using the tcpdump to show on the screen in real time the UCP traffic: tcpdump -l -i bond1 -s 1514 -nntttt -A src or dst 192.168.1.5 and port 10000 | egrep "/51/"The output... (5 Replies)
Discussion started by: Lord Spectre
5 Replies

7. Shell Programming and Scripting

perform some operation on a specific coulmn starting from a specific line

I have a txt file having rows and coulmns, i want to perform some operation on a specific coulmn starting from a specific line. eg: 50.000000 1 1 1 1000.00000 1000.00000 50000.000 19 19 3.69797533E-07 871.66394 ... (3 Replies)
Discussion started by: shashi792
3 Replies

8. Homework & Coursework Questions

How join works and the specific parameters to my problem?

1. The problem statement, all variables and given/known data: I have two files created from extracting data off of two CSV files, one containing class enrollment on a specific quarter and the other containing grades for that specific quarter. The Enrollment file generated contains course name,... (11 Replies)
Discussion started by: Lechnology
11 Replies

9. UNIX for Dummies Questions & Answers

replacing string in a column on a specific line

hi, i currently have a file with columns similar to this customer name owed CID123 John 300 CID342 harry 500 at present i use use awk to find the amount owed by the customer using the customer ID (CID). if the customer spends more money how would i go about using sed/awk etc to... (2 Replies)
Discussion started by: skinnygav
2 Replies

10. Shell Programming and Scripting

replacing specific characters in a string

Hi Friends, Following is an output of a script OPWQERIUTYKLSADFJHGXZNMCVBWQOPERIUTYKLSADFJHGXZNMCVB I want to replace above string's 11 to 17 character by ******* (7 stars) How can it be done? Please somebody guide me. (6 Replies)
Discussion started by: anushree.a
6 Replies
Login or Register to Ask a Question