I have a problem with sed. I want to replace the entire specific line number for multiple lines in multiples documents.
This the bash command for 1 specific line in 1 specific document:
BNAME=$(basename $FILE .pdb)
psfgen1="pdb ./sedpdb/${BNAME}.pdb/"
sed -i '8s/'.*'/'${psfgen1}'/' ./psfgen.inp
And I get this error :
sed: -e expression #1, char 60: unterminated `s' command
Is anyone know how to solve this issue? Thanks!
I can see two things wrong:
There are forward slashes in the string that you're attempting to use in the sed command. These will be interpreted as part of the command, so you should use a different delimiter.
The * is unquoted, so will be glob-expanded by the shell to the names of all the files in the directory.
Reliably using shell variables in string substitutions is non-trivial but can be done using one of the approaches shown in the answers to this question.
In your case, it looks like you can probably get away with using another character as the delimiter, such as #:
sed -i "8s#.*#${psfgen1}#" ./psfgen.inp
Related
I run below sed command
sed -i s/abc=.*$/abc=def ghi/g hpq_sf_attach_wf_param.txt
and it gave me error:
sed: -e expression #1, char 17: unterminated `s' command
I noticed it is due to space in between of def and ghi.
Any idea how to fix it?
You need to use quoting to protect special characters, including spaces, $, and *.
sed -i 's/abc=.*$/abc=def ghi/g' hpq_sf_attach_wf_param.txt
So geekosaur had it right. The the reason you had the problem though is because it needs to be double quotes for the wildcards because with single quotes it takes them as literal characters, not for the meaning you want.
sed -i "s/abc=.*$/abc=def ghi/g" hpq_sf_attach_wf_param.txt
Also if the space between "def" and "ghi" gives you problems, adding a "\" should help making it read it as a literal space.
sed -i "s/abc=.*$/abc=def\ ghi/g" hpq_sf_attach_wf_param.txt
When I try following command, I'd like to rewrite sql.
Day='2020/12/1'
Dir=/home/test/data
sql=`cat $Dir"/"$test".sql" | sed -e "s/Day/$Day/g"`
I suffered following errors.
sed: -e expression #1, char 24: unknown option to `s'
Why the s is recognised as option ? why is this command couldnt work well ?
if someone has opinoin, please let me know
Thanks
The problem is with slashes: your variable contains them and the final command will be something like sed "s/string/path/to/something/g", containing way too many slashes.
Since sed can take any char as delimiter (without having to declare the new delimiter), you can try using another one that doesn't appear in your replacement string like below:
sql=`cat $Dir"/"$test".sql" | sed -e "s|Day|$Day|g"`
Also, you would need to use sed -i to update the file in-place, since it looks like that is what you're trying to do.
I need to replace a string in a lot of files in a folder, with only ssh access to the server. How can I do this?
for example i want replace all files Which contains code
<script src='http://cdn.adplxmd.com/adplexmedia/tags/xbanner/xbanner.js?ap=1300' type='text/javascript'></script>
I want replace it with my name: sultan
I do something like this:
sed -i 's/<script src='http://cdn.adplxmd.com/adplexmedia/tags/xbanner/xbanner.js?ap=1300' type='text/javascript'></script>/sultan/g' *
but the problem i see error message in linux commands:
sed: -e expression #1, char 20: unknown option to `s'
How do I fix this problems?
There are two errors:
the slash is used as a delimiter in your call to sed, so this ambiguity needs to be resolve
you're using single quotes in the search term but also to enclose the sed parameter.
You can try something like this instead:
sed -i "s|<script src='http://cdn.adplxmd.com/adplexmedia/tags/xbanner/xbanner.js?ap=1300' type='text/javascript'></script>|sultan|g" *
Good-day,
In a Bash shell script I'm putting together, I am trying to find this text: /usr/local/freeswitch/log/freeswitch.log and replace it with: /var/log/freeswitch/freeswitch.log in this file: /etc/fail2ban/jail.local
This is what I have tried so far, both of which result in the error: sed: -e expression #1, char 75: unterminated `s' command
Attempt #1
sed -i 's~usr/local/freeswitch/log/freeswitch.log~var/log/freeswitch/freeswitch.log' /etc/fail2ban/jail.local
Attempt #2
sed -i 's~usr/local/freeswitch/log/freeswitch.log/var/log/freeswitch/freeswitch.log' /etc/fail2ban/jail.local
My research shows that since the text I'm searching for includes the "/" character, I should be using a different delimiter "~" to separate the find and replace strings. But looks like I'm doing something wrong, any assistance would be appreciated, thanks.
The structure of a sed substitution command is s/PATTERN/REPLACEMENT/ (note the delimiter at the end of the command).
You're right, you can change the delimiter to a different character, so if you're going to use ~ you need to put one of those at the end of the command.
I am trying to substitute a word using sed with two words. For example I'm using:
sed s/TITLE/New Title/ old.txt > new.txt
However, when I run the command the following populates:
sed: -e expression #1, char 17: unterminated `s' command
Any help would be of great appreciation. I've searched everywhere without any clarity.
You are missing quotes. You need to wrap the substitution portion inside quotes like:
sed 's/TITLE/New Title/' old.txt > new.txt
If you are using variables as part of substitution, you'll need to use double quotes " instead of single quotes ' to allow variables to interpolate.
Take a look at sed man page and explore -i option which allows you to make in-place changes.