Why running sed command cutting my path first letter? - bash

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

Related

deleting two ranges of lines from multiple text files on Mac

On my MacBook Pro, I'm trying to delete two ranges from multiple files in a directory. Searching thus far, it seems like sed is a way to do it, but if I try to delete one of the ranges with the command:
sed -i '5825,6144d' *.tab.txt
I get the error message:
sed: 1: "cl9_408230953ref_f00507 ...": command c expects \ followed by text
Alternatively, I've tried other variants where the command seems to work (i.e., no error message), but the files haven't changed. Suggestions?
Use
sed -i '' '5825,6144d' *.tab.txt
Or
sed -i.bak '5825,6144d' *.tab.txt

Using sed to replace the same text line in multiple files

In my current directory (projDir), I have about 41 folders as follows:
ProgOneDir
ProgTwoDir
ProgThreeDir
...
ProgFortyOneDir
...etc (I did not come up with the naming scheme and I am not a systems admin so please don't yell at me for them).
Inside each of the subfolders, there are README.md files I need to edit. Instead of going into each subfolder and editing the files there, I would like to use a sed command to do so. The files are consistently named README.md
I am running CentOS7
My current command is:
find . -name 'README.md' -exec sed -i -e 's/./makeprog $MAKE_FLAGS CFLAGS="-I/usr/local/include/libtool" OFLAGS="-L/usr/local/lib"/./makeprog/g' {} \;
Essentially, I need to switch:
./makeprog $MAKE_FLAGS CFLAGS="-I/usr/local/include/libtool" OFLAGS="-L/usr/local/lib
to
./makeprog
Would somebody be able to assist? The error I get is:
sed: -e expression #1, char 43: unknown option to `s'
I have already looked at:
Using sed to replace text between strings
How to replace a path with another path in sed?
Some other online resources
I believe my error is arising with the ./command. I have followed advice of switching all / to +, but that still didn't work, as I got the following error:
sed: -e expression #1, char 110: unterminated `s' command
Thank you.
Your problem is that you are using slash as the delimiter to the s/// command, but you are putting slashes into the pattern. You can pick a different delimiter that does not appear in the pattern.
Assuming your current directory is projDir, and there is only one call to makeprog:
sed -i -E 's#(./makeprog) .*#\1#' */README.md
or
perl -i -e 's{./makeprog\K.*}{}' */README.md
If you want to specifically match that exact line, then:
# are you missing a trailing double quote?
line='./makeprog $MAKE_FLAGS CFLAGS="-I/usr/local/include/libtool" OFLAGS="-L/usr/local/lib'
sed -i -E "s#$line#./makeprog#" */README.md

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.

Using SED to remove line from textfile

I had a look around the internet for a line of code which would delete the whole line it found a match on.. So if it found file in the file below it would delete the whole file line.. i.e '13382748 | /root/file', which it does in the console...
/root/file:
13382748 | /root/file
13382749 | /root/test
The command below works in the console (as stated above) but does not work when running the script.
sed --in-place '/$number/d' /root/file
It is the last piece to finally complete my script. The line of code simply does nothing..
Any ideas?
The single quotes prevent variable expansion. Say:
sed --in-place "/$number/d" /root/file

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