LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to use sed command (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-use-sed-command-4175690291/)

sncak 02-11-2021 02:07 AM

how to use sed command
 
I wanted to replace regex
Code:

-o '-p -- \\u'
with
Code:

--autologin wizard
using sed , i tried using
Code:

sed "s:-o /'-p -- \\u'/:--autologin wizard:" FILE
command but not working. can anyone suggest the best solution to this?

shruggy 02-11-2021 02:34 AM

Quoting is the problem, particularly handling of \\ inside double quotes. Try
Code:

echo "s:-o /'-p -- \\u'/:--autologin wizard:"
and you'll see what I mean. Adding another backslash or two would help:
Code:

echo "s:-o /'-p -- \\\u'/:--autologin wizard:"
Besides, there are no forward slashes (/) in the string you're trying to replace, but they are present in the sed expression?

sncak 02-11-2021 02:55 AM

I've tried the solution you have provided but it is not working.

Code:

sed "s:-o /'-p -- \\\u'/:--autologin wizard:"
I am still getting the unchanged output.

shruggy 02-11-2021 03:03 AM

Remove the forward slashes
Code:

sed "s:-o '-p -- \\\u':--autologin wizard:"

MadeInGermany 02-13-2021 08:30 AM

If you remember the rules

In the shell within a 'string in ticks'
  • a literal \ is a \
  • a literal ' is escaped '\''
And
  • a 'string in ticks' becomes a one-token string in ticks
And in sed
  • a literal \ is escaped \\
then it is straight forward
Code:

sed 's#-o '\''-p -- \\\\u'\''#--autologin wizard#'
sed gets
Code:

s#-o '-p -- \\\\u'#--autologin wizard#
and it substitutes
Code:

-o '-p -- \\u'
with
Code:

--autologin wizard

JJJCR 02-13-2021 09:10 AM

try this:

Quote:

sed -i -e 's/\\\\u/:--autologin wizard:/g' FILE


All times are GMT -5. The time now is 07:37 PM.