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

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

Related

Using sed to substitute string in in double quote with a bash variable

I have a yaml file that I am trying to create a number within a loop and replace it with the newly computed variable.
The line i wish to replace looks like this:
# genesis_gas_limit: "16000000" ## Used to set genesis gas limit
I plan on putting this in a loop and thus I would love it if the sed could take a wild card i.e.
# genesis_gas_limit: "*" ## Used to set genesis gas limit
I have have tried this
sed -i 's/ genesis_gas_limit:/c\ genesis_gas_limit: $GASLIMIT' examples/values-local.yaml
but i get the following error:
sed: 1: "examples/values-local.yaml": invalid command code e
I would appreciate any pointers on this
You could replace genesis_gas_limit: and whatever content follows it, with genesis_gas_limit: and the new value. Using GNU sed:
sed -i "s/ genesis_gas_limit:.*/ genesis_gas_limit: \"$GASLIMIT\"/" examples/values-local.yaml
Using BSD sed (in OSX):
sed -i.bak -e "s/ genesis_gas_limit:.*/ genesis_gas_limit: \"$GASLIMIT\"/" examples/values-local.yaml

Changing a (full pathname) line using sed `s` operation

I'm trying to change #!/usr/bin/python to #!/usr/bin/python2.6
I've tried the following command line:
sed -i -e 's/.#!/usr/bin/python.*/#!/usr/bin/python2.6/' /usr/bin/yum
...which returns the following error:
sed: -e expression #1, char 11: unknown option to `s'
I can't find a good answer anywhere on Google.
Thanks in advance.
You're getting this error because sed interprets s/.#!/usr/b as your first search & replace command, and b isn't a valid flag for the command.
Indeed, the syntax of these commands is s<delimiter><search pattern><delimiter><replace><delimiter><flags>, where the most widely used <delimiter> is /.
You could escape the / in your search pattern and replace string so they aren't interpreted as the delimiter, however since you've got a lot of them I would suggest using another delimiter.
For example, using + as a delimiter your sed command would become sed -i -e 's+.#!/usr/bin/python.*+#!/usr/bin/python2.6+'.
As a side-note, the first . in your command is probably a mistake as the shebang should be written directly at the beginning of the files it appears in.
I assume that your line is first line from your file. With GNU sed:
sed '1s/python$/&2.6/' file
If you want to edit your file "in place" use sed's option -i.

How to remove line matching specific pattern from a file

I know sed could be used to delete specific line from file:
sed -i "/pattern/d" file
While the pattern of my case includes slash, like /var/log,
So I know I need escape: sed -i "/\/tmp\/dir/d" file
However, for my case, the pattern is dynamic, should be a variable
in a shell file, so I have to convert the variable value to replace
"/" with "\\/", then got this:
sed -i "/^${pattern_variable//\\//\\\\\\/}$/d" file
My question is, is there any better implementation which is more readable or simpler? Not only sed, other utility is also acceptable. Is it possible to handle not only slash but also other various symbols, like backslash or # ()?
you can use char other than /:
sed "\#$varHasSlash#d"
example:
kent$ foo="b/c"
kent$ echo "a
ab/cd
e"|sed "\#$foo#d"
a
e

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 replace command

I want to uncomment one of these two statements as per condition:
#export CATALINA_OPTS="-javaagent:$TOMCAT_INSTALL_LOCATION/$APACHE_TOMCAT_VERSION/lib/oi-mule-libs/usage/injector.jar"
#export CATALINA_OPTS="-javaagent:$TOMCAT_INSTALL_LOCATION/$APACHE_TOMCAT_VERSION/lib/agents/cl-proxy-agent.jar"
I used following sed command:
#sed -i 's/#export CATALINA_OPTS="-javaagent:$TOMCAT_INSTALL_LOCATION/$APACHE_TOMCAT_VERSION/lib/oi-mule-libs/usage/injector.jar"/export CATALINA_OPTS="-javaagent:$TOMCAT_INSTALL_LOCATION/$APACHE_TOMCAT_VERSION/lib/oi-mule-libs/usage/injector.jar"/' $TOMCAT_HOME/bin/init-script.sh
I got following error:
sed: -e expression #1, char 85: unknown option to `s'
Why did I get this error?
How could I achieve my purpose by modifying above command?
You should change your delimeters to the sed command. Your delimiter is '/' which also appers in your pattern and that gives an error. Try this:
sed 's:<pattern to be replaced>:<replacement pattern>:g'
Here g is for global, s for substitute. You can use any other delimiter not appearing in your pattern, I have used colon ':' in the example
You need to either escape slash characters with backslashes or use another character as a delimiter for the "s"ubstitute command (as pointed out by sleeping_dragon)
With that in mind:
sed -e s/^#(.*\/injector.jar.*)/\1/ -e s/^#(.*\/cl-proxy-agent.jar.*)/\1/ $my_file
making the regular expression in round brackets as unique as required to identify the exact string you need uncommenting.

Resources