replace some string after certain other string with sed - bash

In my config file I have an asset version like:
templating:
engines: ['twig']
assets_version: 20161021
When i plan to make a new release i want to replace current version (in this case 20161021) in a bash script with new version (for example with current date).
i have searched long time for examples of sed use but without success.
My Code:
sed -e 's/(::\s+assets_version:)[^=]*$/\1 assets/' app/config/config.yml
But console told me: sed: 1: "s/(::\s+assets_version: ...": \1 not defined in the RE
Can anybody help?

VERSION=20161101
sed -i "s/assets_version.*/asserts_version: ${VERSION}/" app/config/config.yml
-i to change on the same file
assets_version.* matches assets_version and any more character on the line then replace them.

Related

Why running sed command cutting my path first letter?

sed -i s/CUSTOMER_UNIT=".*"/CUSTOMER_UNIT="Test" core/src/main/java/com/appname/core/AppConstants.kt
I am running this sed command as a result I get this
sed: ore/src/main/java/com/appname/core/AppConstants.kt: No such file or directory
It remove the first letter of core -> ore
but if I just run command find ore/src/main/java/com/appname/core/AppConstants.kt it this file exists
I am not sure if you are trying to have the results = core or ore
but when i replicate I am able to get core. One thing I noticed is in your example you are missing the terminating / for your sed command after "Test"
sed -i s/CUSTOMER_UNIT=".*"/CUSTOMER_UNIT="Test"/ core/src/main/java/com/appname/core/AppConstants.kt
sed: can't read core/src/main/java/com/appname/core/AppConstants.kt: No such file or directory
if you are trying to cut the C from core and you can manually validate the path exists I would be sure that you have proper permissions
The command should be enclosed in quotes and be ended with "/".
sed -i 's/CUSTOMER_UNIT=".*"/CUSTOMER_UNIT="Test"/' core/src/main/java/com/appname/core/AppConstants.kt

Replacing the file name which comes with # (Hash) using sed

I facing some problem with replace the file name mixed with #
Ex:- #test#123
i have maintaining the report file like report.csv, Im using the bash script to summarize my report.
by default my report.csv file comes with all extension type as a default template.
Inside my report.csv
DATE-COPY COPY_STATUS NO_TXT NO_ERR NO_SQL
if my copy success i will replace using sed from COPY_STATUS to COPY_SUCCESS
if its normal file(/home/user/text123.txt) is copied i dont have problem with replacing path
sed 1s#NO_TXT#/home/user/text123.txt# -i report.csv
after that my csv file will look
DATE-COPY COPY_SUCCESS /home/user/text123.txt NO_ERR NO_SQL
if i have # hashmixed file
example
sed 1s#NO_TXT#/home/user/#text#123.txt# -i report.csv
i get like below error and unable to replace hash files with full path
sed: -e expression #1, char 44: unknown option to `s'
Sed can use a number of characters for its separator. Most commonly a / is used, however for files it's handy to use another character such as #, as you have done. In your instance, the simplest approach would be to use another separator such as |, but ensure you quote your sed string:
sed '1s|NO_TXT|/home/user/#text#123.txt|' -i report.csv
Or if you are passing in a variable, use double quotes:
sed "1s|NO_TXT|/home/user/$file|" -i report.csv

bash - replacing string with sed

For some mysterious reason, some elements in my CSV data appear as s/stWgvN52??f2& ?" instead of stWgvN522tw0JtZZnyXj, which messes up the file because I have ; set as the CSV delimiter.
I attempted to replace the defective string using sed as follows:
$ sed -i 's/stWgvN52??f2& ?"/stWgvN522tw0JtZZnyXj/g' file.csv
but I get the following error:
sed: 1: "access_logs_2014-04.csv": command a expects \ followed by text
What is the reason?
When you use the -i option, you have to specify the extension of the backup file that gets made. Some versions of sed expect the extension directly appended to the -i option, so what you wrote would work. But other versions (like the version on OS X) require it to be a separate option, so you have to write:
sed -i '' 's/stWgvN52??f2& ?"/stWgvN522tw0JtZZnyXj/g' file.csv
to specify that you don't want a backup file.

Sed gives "sed: 1: "tsunit.js": undefined label 'sunit.js'"

Short questing, why does this line
sed -i '1s/^/#!\/usr\/bin\/env node\n/' tsunit.js;\
Give me this error
sed: 1: "tsunit.js": undefined label 'sunit.js'
in a Makefile, if relevant.
I’m on a Mac.
According to the Apple man page for sed, the -i option takes a required argument specifying the file extension for the backup file. As a result, assuming that you are on a Mac or similar, sed believes that you intended '1s/^/#!\/usr\/bin\/env node\n/' to be the file extension of the backup. It then interprets tsunit.js as a sed command. the leading t tells sed to branch to the label sunit.js which, of course, doesn't exist. Hence the error message.
The solution is:
sed -i '.bak' '1s/^/#!\/usr\/bin\/env node\n/' tsunit.js
Or, if you really do not want a backup:
sed -i '' '1s/^/#!\/usr\/bin\/env node\n/' tsunit.js
Also, it looks like you're inserting a line. sed has more commands than s
sed -i "" '1i\
#!/usr/bin/env node' tsunit.js

Replace text in a file using sed on windows

I am trying to replace a string in a .ism file using the SED command but it is failing for me.
The line in the file is the product version which I want to change in every build.
<row><td>ProductVersion</td><td>1.0.0</td><td/></row>
And here is the command I'm trying:
c:\rt_cygwin\bin\sed -i -r "s/ProductVersion\\"+[0-9].+[0-9].+[0-9]
/ProductVersion\"1.0.%BUILD_NUMBER%/" "D:\lm.ism"
Thanks
c:\rt_cygwin\bin\sed -i -r "s/ProductVersion</td>[0-9].[0-9].[0-9]/ProductVersion 1.0.%BUILD_NUMBER%/" "D:\lm.ism"
You have extra quotes that is not in the example line that should be removed and as #jeb pointed out the portion is missing.

Resources