How to remove all occurences of string "???" of a file and save it?
My approach so far:
cat file.txt | sed -ie '/s/???//' file.txt
However I get the following error:
sed: -e expression #1, char 4: unknown command: `?'
You can use this sed command:
sed -i 's/???//g' file.txt
There is no reason to use cat here as sed can directly operate on a file and save it in-line.
Also note that unlike other regex flavors BRE (Basic Regular Expressions) which is default regex engine of sed doesn't treat ? as a special regex meta character hence there is no need to escape ? here.
Related
I want to replace with sed in some bash script something like:
s:44:\"STRING\"
To:
s:NEWSTRING:\"NEWSTRING2\"
I tried many ways with escaping special characters, but I got always error
sed: -e expression #1, char 32: unterminateds' command`
or someting like that.
Can you please tell me the correct sed -i (sed -i "s/xxx/xxx/g" file) command for that?
You have to escape the backslashes properly:
sed 's/s:44:\\"STRING\\"/s:NEWSTRING:\\"NEWSTRING2\\"/'
Example:
$ echo 's:44:\"STRING\"' | sed 's/s:44:\\"STRING\\"/s:NEWSTRING:\\"NEWSTRING2\\"/g'
s:NEWSTRING:\"NEWSTRING2\"
You are missing the final delimiter. In your case it seems to be : therefore, you need to add a final : after your substitution content. It does not matter if you are using modification instruction like g
What I have:
test
more text
#user653434 text and so
test
more text
#user9659333 text and so
I'd like to filter this text and finally get the following list as .txt file:
user653434
user9659333
It's important to get the names without "#" sign.
Thx for help ;)
Using grep -P (requires GNU grep):
$ grep -oP '(?<=#)\w+' File
user653434
user9659333
-o tells grep to print only the match.
-P tells grep to use Perl-style regular expressions.
(?<=#) tells sed that # must precede the match but the # is not included in the match.
\w+ matches one or more word characters. This is what grep will print.
To change the file in place with grep:
grep -oP '(?<=#)\w+' File >tmp && mv tmp File
Using sed
$ sed -En 's/^#([[:alnum:]]+).*/\1/p' File
user653434
user9659333
And, to change the file in place:
sed -En -i.bak 's/^#([[:alnum:]]+).*/\1/p' File
-E tells sed to use the extended form of regular expressions. This reduces the need to use escapes.
-n tells sed not to print anything unless we explicitly ask it to.
-i.bak tells sed to change the file in place while leaving a backup file with the extension .bak.
The leading s in s/^#([[:alnum:]]+).*/\1/p tells sed that we are using a substitute command. The command has the typical form s/old/new/ where old is a regular expression and sed replaces old with new. The trailing p is an option to the substitute command: the p tells sed to print the resulting line.
In our case, the old part is ^#([[:alnum:]]+).*. Starting from the beginning of the line, ^, this matches # followed by one or more alphanumeric characters, ([[:alnum:]]+), followed by anything at all, .*. Because the alphanumeric characters are placed in parens, this is saved as a group, denoted \1.
The new part of the substitute command is just \1, the alphanumeric characters from above which comprise the user name.
Here, the s indicates that we are using a sed substitute command. The usual form
With GNU grep:
grep -Po '^#\K[^ ]*' file
Output:
user653434
user9659333
See: The Stack Overflow Regular Expressions FAQ
I am using shell script. My requirement is to find and replace the string. The string contains "/" char as well. I am getting error sed: -e expression #1, char 18: unterminated `s' command. Can someone tell how should i replace the string which has "/"?
#!/bin/bash
...
search_string="../conf/TestSystem/Inst1.xml"
rep="Inst1/Instrument.xml"
sed -i 's|${line}|${rep}/g' MasterConfiguration.xml
I tried using another sed command but that one also gave error sed: -e expression #1, char 13: unknown option to `s'
sed -e "s/${line}/${rep}/g" MasterConfiguration.xml > tempfile
Whenever you deal with shell-variables you have to get them out of the "sed-string":
For example:
sed -e "s/"${line}"/"${rep}"/g" MasterConfiguration.xml > tempfile
Otherwise sed will treat the chars as-is and search for ${line} literally:
As you see, nothing happens here.
Furthermore, if your variables contain / you need to use another delimiter for sed. I tend to use ~ in such a case, but you're free to use other chars - just be consequent and don't mix them like in your first example-sed-command:
sed 's~'${line}'~'${rep}'/g' //WRONG
sed 's~'${line}'~'${rep}'~g' //RIGHT
Combine both and it will work:
You can try this sed,
sed -i "s#${line}#${rep}#g" MasterConfiguration.xml
Problem:
Instead you have,
sed -i "s|${line}|${rep}/g" MasterConfiguration.xml
It should be,
sed -i "s|${line}|${rep}|g" MasterConfiguration.xml
Syntax:
sed "s|pattern|replacement|g"
I have a problem replacing default password hash in config file:
sed -i 's/default_password_crypted: "[^"]*"/default_password_crypted: "\$1\$mF86/UHC\$WvcIcXred6crBz2onWxyac."/' input.txt
i get following error:
sed: -e expression #1, char 74: unknown option to `s'
works:
search pattern: default_password_crypted: "$1$mF86/UHC$WvcIcX2t6crBz2onWxyac."
sed -i 's/default_password_crypted: "[^"]*"/default_password_crypted: "1234567890"/' input.txt
how do i need to write replace pattern for hash ?
thx
You need to escape the literal / inside your replacement as it’s the delimiter:
sed -i 's/default_password_crypted: "[^"]*"/default_password_crypted: "\$1\$mF86\/UHC\$WvcIcXred6crBz2onWxyac."/' input.txt
Or simply use a different character, for example a ,:
sed -i 's,default_password_crypted: "[^"]*",default_password_crypted: "\$1\$mF86,UHC\$WvcIcXred6crBz2onWxyac.",' input.txt
You also don’t need to escape the $ inside the replacement.
I need to remove escape character from the string in bash. I get a data structure, which contains url paths with / escaped so I receive the regular link:
http://stackoverflow.com/questions/ask
as one with escaped /:
http:\/\/stackoverflow.com\/questions\/ask
Now I need to remove \ from the second link. For this purpose I tried using sed
`echo '"'${paths[$index]}'"' | sed "s#\\##g"`
But I get an error:
sed: -e expression #1, char 6: unterminated `s' command
If I replace \\ with ie. _ it works like a charm and removes all occurrences of _ in a string. How do I get rid of escape characters in a string using sed?
try this:
.......|sed 's#\\##g'
or:
.......|sed "s#\\\\##g"
EDIT add a test output:
kent$ echo "http:\/\/stackoverflow.com\/questions\/ask"|sed "s#\\\\##g"
http://stackoverflow.com/questions/ask
kent$ echo "http:\/\/stackoverflow.com\/questions\/ask"|sed 's#\\##g'
http://stackoverflow.com/questions/ask
Your question isn't clear about which way round you want so here is both ways:
$ sed 's#/#\\/#g' <<< "http://stackoverflow.com/questions/ask"
http:\/\/stackoverflow.com\/questions\/ask
$ sed 's#\\/#/#g' <<< "http:\/\/stackoverflow.com\/questions\/ask"
http://stackoverflow.com/questions/ask
You don't need to use sed.
paths[index]=${paths[index]//\\/}
or simply
echo ${paths[index]//\\/}
to see the result without modifying the value in-place.
You can use this :
sed 's#\\##g'
But the problem is when you encounter a backslash that you actually want in the string, but is escaped. In that case :
sed 's/\\\\/\x1/' |sed 's/[\]//g' | sed 's/\x1/\\/g'
Replaces the double backslash with with a temp character[SOH], replaces all other backslashes and then restores the backslash that is needed.