This question already has answers here:
sed edit file in place
(15 answers)
Closed 4 years ago.
I'm learning SED and I saw this sed substitution example. It's supposed to replace the first lowercase t as uppercase in each new line.:
$ sed 's/t/T' text-01.txt
sed: -e expression #1, char 5: unterminated `s' command
Contents of file:
$ cat text-01.txt
10 tiny toes
this is that
5 funny 0
one two three
tree twice
It's not the end of the world though, since I can just output into a new file:
cat text-01.txt | sed 's/t/T/' > text-02.txt
But what am I supposed to do if I want to edit the original file?
The commands aren't the same, the closing / is missing in the first one:
# v
sed 's/t/T' text-01.txt
cat text-01.txt | sed 's/t/T/' > text-02.txt
# ^
Related
This question already has answers here:
sed fails with "unknown option to `s'" error [closed]
(1 answer)
How to pass a variable containing slashes to sed
(7 answers)
Closed 2 years ago.
I am trying to replace a variable value in my file using bash.
Input string ----- preprocess_date=06/24/2020_17:00
Expected string ----- preprocess_date=06/24/2020_17:10
I have the value 06/24/2020_17:10 in prep_tmp variable. I have tried the below command:
sed -i s/preprocess_date=.*/preprocess_date=${prep_tmp}/
I'm getting the error sed: -e expression #1, char 43: unknown option to `s'
Can anyone help me on this?
Thanks in advance.
Your command with the 3 seperating /:
sed -i s/preprocess_date=.*/preprocess_date=${prep_tmp}/
^ ^ ^
expands to:
sed -i s/preprocess_date=.*/preprocess_date=06/24/2020_17:10/
^ ^ ^
with 24/2020_17:10/ overflowing after the s command
To solve this problem you can replace all the sperarating / with some other character. I like using the | (but also needs " to ensure that they are not parsed as pipes by a shell)
sed -i "s|preprocess_date=.*|preprocess_date=${prep_tmp}|"
This question already has answers here:
How to pass a variable containing slashes to sed
(7 answers)
Closed 1 year ago.
My variables issuer and subject contains
issuer=C=IN,ST=TN,L=XYZ,O=ABC,OU=CA,CN=XYZ.com/emailAddress=xyz#gmail
subject=C=GB,ST=London,L=London,O=Global_Security,OU=IT,CN=example.comCA/emailAddress=acl#123
I want to replace a variable leftid and leftca with subject and issuer in xyz.conf respectively.
I'm using sed command like this
sed -i -e '/leftid=/s/=.*/= '$subject'/' /etc/xyz.conf
and
sed -i -e '/leftca=/s/=.*/= '$issuer'/' /etc/xyz.conf
But I'm getting the following error(s)
sed: -e expression #1, char 66: unknown option to s' sed: -e
expression #1, char 83: unknown option tos'
Can anyone help?
Your $issuer variable is expanded in the sed substitution part and so the expression becomes:
sed -i -e '/leftca=/s/=.*/= 'C=IN,ST=TN,L=XYZ,O=ABC,OU=CA,CN=XYZ.com/emailAddress=xyz#gmail'/' /etc/xyz.conf
and this is not a correct sed expression since there is 4 occurrences of the / delimiter.
To sum up your sed command is right just change the s substitution command separator with something not present in your issuer variable, like for instance:
sed -i -e '/leftid=/ s|=.*|= '$subject'|' /etc/xyz.conf
This question already has answers here:
How to pass a variable containing slashes to sed
(7 answers)
Closed 1 year ago.
I have this as variable
set var=1920 x 1080p / 23.976 fps /16:9 / High Profile 4.1
and i will replace with sed
sed -e "s/1920/%var%/" movie.txt > movie2.txt.nfo
error output is
ed: -e expression #1, char 30: unknown option to `s'
I do not understand what's wrong
Regards
The problem is with slashes. Your final command will have many slashes coming from the variable itself, which will be confused with the / delimiter you provided in the sed command itself.
Luckily, sed can have any char as the delimiter. Change to something like:
sed -e "s-1920-%var%-"
Now, - won't be confused with / (from the variable).
This question already has answers here:
How to use sed to replace only the first occurrence in a file?
(25 answers)
Closed 8 years ago.
I am using sed command make substitution in the file.
Assume my file1 is:
10
11
10
11
Then I want to substitute the 1st "10" in file1 to "12", and dump to file2. The file2 should be:
12
11
10
11
I tried this command:
sed 's/10/12/' file1 >file2
But it changed the 2nd "10" also. So, how can I write the sed command to do that?
If you can use awk instead of sed, you can have more control like this:
awk '!changed && /10/ { sub(/10/, "12"); changed = 1}1' file1
12
11
10
11
try:
sed '0,/10/s/10/12/' file1 >file2
Like GriffinG said, you can do something like this:
sed '0,/10/ s/10/12/' file1 > file2
The 0,/10/ at the beginning sets the bounds on the following substitution command. It says start from line 0 (the first line), and go until a line it matches the pattern /10/. So After that first match, sed will stop processing that substitution.
If you do not have GNU sed, try:
echo | cat - file | sed '1,/10/s/10/12/; 1d'
or
sed '1,/10/s/10/12/; 1d' << EOF
$(cat file)
EOF
or in bash / ksh93
sed '1,/10/s/10/12/; 1d' <(echo; cat file)
GNU sed knows 0,/10/ so the extra empty line is not required
This question already has an answer here:
sed fails with "unknown option to `s'" error [closed]
(1 answer)
Closed 7 years ago.
I have file .config with line :
projdir name_of_the_projdir
I need to with replace name_of_the_projdir with my own variable. I try to do it with
sed -i 's/^projdir .*$/projdir '$projdir'/' .le/.config
but i get error
sed: -e expression #1, char 25: unknown option to `s'
anyone know what to do ?... i try to replace first ' and last with " and first and alst / with # . But still not work
You get that error because the / delimiters you use with the substitute command are clashing with the directory separators. You can change them to something else:
sed -i 's!^projdir .*$!projdir '$projdir'!' .le/.config