Replacing Value (URL) in Properties file in Unix - unix

I am trying to replace value of the property file in unix using Sed command. Here is the code:
Properties file:
Test.abc.homeURL:https://www.abdfghjc.net/instant-cash-offer/?from=1&LNX=TIMATCMOB&oCode=CMB
Shell:
MOBILE_HOME_PAGE_KEY="Test.abc.homeURL"
MOBILE_REPLACE_URL="http://ww.xyz.com/default.html"
sed -i
"s/^$MOBILE_HOME_PAGE_KEY:.*/$MOBILE_HOME_PAGE_KEY:$MOBILE_REPLACE_URL/"
propertiesFile
Error:
sed: -e expression #1, char 72: unknown option to `s'
Looks like some regular expression error, couldn't figure it out. please help me?
Thanks,

You have to use a different character than slash (/) to separate the sed parameters, because you have slashes in one of the variables ($MOBILE_REPLACE_URL).
Just use another character like #. You also couldn't use colon because you have colon in your variables.

As far as I understood , your requirement is to replace the key MOBILE_HOME_PAGE_KEY with MOBILE_REPLACE_URL key's value.
Below is what I tried
sed -e 's/^MOBILE_HOME_PAGE_KEY=.*/MOBILE_HOME_PAGE_KEY=\"http://ww.xyz.com/default.html\"/' newfile
newfile is the filename

Related

Find and replace: \'

I'm trying to replace a every reference of \' with ' in a file
I've used variations of: sed -e s/\'/"\'"/g file.txt
But they always replace every.single.(single).quote
Any help would be greatly appreciated.
Not sure it's the best solution,I could do it like this:
sed "s/[\]'/\"\'\"/g" file.txt
(putting the backslash character in a character range so it doesn't interfere with the following quote, and protect with double quotes)
Or just extending your syntax, without quotes but using almost the same trick:
sed -e s/[\\]\'/"\'"/g file.txt
An approach trying to conserve as much of the "single-quotedness" of the sed command as possible:
sed 's/\\'"'"'/\'/g'
Just escaping \ with \\ and getting a single quote into the command with '"'"': the first single quote ends the command so far, then we have a double-quoted single quote ("'"), and finally an opening single quote for the rest of the command.
Alternatively, double quoting the whole command and escaping both the backslash and single quote:
sed "s/\\\'/\'/g"
The correct syntax is:
$ echo "foo'bar" | sed 's/'\''/\'/'
foo'bar
Every script (sed, awk, whatever) should always be enclosed in single quotes and you just us other single quotes to stop/restart the script delimiters break out to shell for the minimal portion of the script that's absolutely necessary, in this case long enough to use \'. You need to break out to shell to specify that ' because per shell rules no script enclosed in 's can contain a ', not even if you try to escape it.
echo "foo'bar" | gawk '{gsub(/\47/,"\\'")}1'
foo'bar
The tricky part here is to replace a single quote with ampersand.
First in order to make the single quote manageable use its octal
code here \47 and then escaping ampersand by two back slash. And all of sudden
it becomes feasible :)

sed command does not work when a comma is present

New to sed, so please bear with me...
I have a php file which contains the following line:
define('TARGET_A','044');
Id like to find that line and replace it with the following using sed:
define('TARGET_K','076');
I have tried:
$ sed -i 's/define\(\'TARGET_A\',\'044\'\)\;/define\(\'TARGET_K\',\'076\'\)\;/' myfile.php
I have tried SEVERAL variations, tried escaping the parens and removing the semicolon, nothing seems to work
ANY help at all GREATLY appreciated, thanks
That's a lot of escaping. How about... no escaping at all?
sed -i '.bak' "s/define('TARGET_A','044');/define('TARGET_K','076');/" myfile.php
Example:
cternus#astarael:~⟫ cat myfile.php
define('TARGET_A','044');
cternus#astarael:~⟫ sed -i '.bak' "s/define('TARGET_A','044');/define('TARGET_K','076');/" myfile.php
cternus#astarael:~⟫ cat myfile.php
define('TARGET_K','076');
This worked for me:
$sed -i "s/define('TARGET_A','044');/define('TARGET_K','076');/" myfile.php
I changed the argument string delimiter to make it simpler.
You can't escape 's in a '-delimited script so you need to escape back to shell with '\'' whenever you need a '. You might be tempted to use " to delimit the script instead but then you're opening it up to shell variable expansion, etc. so you need to be careful about what goes in your script and escape some characters to stop the shell from expanding them. It's much more robust (and generally makes your scripts simpler) to just stick to single quotes and escape back to shell just for the parts you NEED to:
$ sed 's/define('\''TARGET_A'\'','\''044'\'');/define('\''TARGET_K'\'','\''076'\'');/' file
define('TARGET_K','076');

Delete line containing a specific string starting with dollar sign using unix sed

I am very new to Unix.
I have a parameter file Parameter.prm containing following lines.
$$ErrorTable1=ErrorTable1
$$Filename1_New=FileNew.txt
$$Filename1_Old=FileOld.txt
$$ErrorTable2=ErrorTable2
$$Filename2_New=FileNew.txt
$$Filename2_Old=FileOld.txt
$$ErrorTable3=ErrorTable3
$$Filename3_New=FileNew.txt
$$Filename3_Old=FileOld.txt
I want get the output as
$$ErrorTable1=ErrorTable1
$$ErrorTable2=ErrorTable2
$$ErrorTable3=ErrorTable3
Basically, I need to delete line starting with $$Filename.
Since $ is a keyword, I am not able to interpret it as a string. How can I accomplish this using sed?
With sed:
$ sed '/$$Filename/d' infile
$$ErrorTable1=ErrorTable1
$$ErrorTable2=ErrorTable2
$$ErrorTable3=ErrorTable3
The /$$Filename/ part is the address, i.e., for all lines matching this, the command following it will be executed. The command is d, which deletes the line. Lines that don't match are just printed as is.
Extracting information from a textfile based on pattern search is a job for grep:
grep ErrorTable file
or even
grep -F '$$ErrorTable' file
-F tells grep to treat the search term as a fixed string instead of a regular expression.
Just to answer your question, if a regular expression needs to search for characters which have a special meaning in the regex language, you need to escape them:
grep '\$\$ErrorTable' file

sed: remove digits after word

I have a simple sed question.
I have data like this:
2600,Sale,"Approved 911973",244.72
2601,Sale,"Approved 04735C",490.51
2602,Sale,"Approved 581068",52.82
2603,Sale,"Approved 009275",88.10
How do I make it like this:
2600,Sale,Approved,244.72
2601,Sale,Approved,490.51
2602,Sale,Approved,52.82
2603,Sale,Approved,88.10
Notice the numbers after approved are gone as well as the quotes. I can remove quotes with:
sed 's/,$//gn' file
but I don't know how to remove the spaces and digits.
Thanks!
sed "s/\"Approved[^,]*/Approved/g"
It finds the quoted "Approved" followed by any non-comma character, up until the first comma encountered, and replaces it with Approved (no quotes)
2600,Sale,Approved,244.72
2601,Sale,Approved,490.51
2602,Sale,Approved,52.82
2603,Sale,Approved,88.10
Using extended regex with sed:
sed -r 's/"([^[:space:]]*)[^"]*"/\1/g' file
The above regex targets for any quoted string. If you want to target the string Approved, then:
sed -r 's/"(Approved)[^"]*"/\1/g' file
With basic regex:
sed 's/"\(Approved\)[^"]*"/\1/g' file
To target any quoted string, change Approved to [^[:space:]]*
One way using awk(only if the other columns does not contain multiple words as in your sample):
awk -F"[ ,]" '{gsub("\"","");$1=$1}1' OFS=, file
awk -F'[," ]' '{OFS=","; print $1,$2,$4,$7}' file
Output:
2600,Sale,Approved,244.72
2601,Sale,Approved,490.51
2602,Sale,Approved,52.82
2603,Sale,Approved,88.10
I suppose there is no other whitespace.

How do I replace a token with the result of `pwd` in sed?

I'm trying to do something like this:
sed 's/#REPLACE-WITH-PATH/'`pwd`'/'
Unfortunately, I that errors out:
sed: -e expression #1, char 23: unknown option to `s'
Why does this happen?
You need to use a different character instead of /, eg.:
sed 's?#REPLACE-WITH-PATH?'`pwd`'?'
because / appears in the pwd output.
in sed, you can't use / directly, you must use '/'.
#!/bin/bash
dir=$`pwd`/
ls -1 | sed "s/^/${dir//\//\\/}/g"
sed 's:#REPLACE-WITH-PATH:'`pwd`':' config.ini
The problem is one of escaping the output of pwd correctly. Fortunately, as in vim, sed supports using a different delimiter character. In this case, using the colon instead of slash as a delimiter avoids the escaping problem.
instead of fumbling around with quotes like that, you can do it like this
#!/bin/bash
p=`pwd`
# pass the variable p to awk
awk -v p="$p" '$0~p{ gsub("REPLACE-WITH-PATH",p) }1' file >temp
mv temp file
or just bash
p=`pwd`
while read line
do
line=${line/REPLACE-WITH-PATH/$p}
echo $line
done < file > temp
mv temp file

Resources