LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-11-2021, 02:07 AM   #1
sncak
LQ Newbie
 
Registered: Feb 2021
Posts: 13

Rep: Reputation: Disabled
Question 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?

Last edited by sncak; 02-11-2021 at 02:50 AM.
 
Old 02-11-2021, 02:34 AM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,677

Rep: Reputation: Disabled
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?

Last edited by shruggy; 02-11-2021 at 02:41 AM.
 
Old 02-11-2021, 02:55 AM   #3
sncak
LQ Newbie
 
Registered: Feb 2021
Posts: 13

Original Poster
Rep: Reputation: Disabled
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.
 
Old 02-11-2021, 03:03 AM   #4
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,677

Rep: Reputation: Disabled
Remove the forward slashes
Code:
sed "s:-o '-p -- \\\u':--autologin wizard:"
 
Old 02-13-2021, 08:30 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,816

Rep: Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211Reputation: 1211
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

Last edited by MadeInGermany; 02-13-2021 at 08:40 AM.
 
Old 02-13-2021, 09:10 AM   #6
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 2,162

Rep: Reputation: 449Reputation: 449Reputation: 449Reputation: 449Reputation: 449
try this:

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


Reply

Tags
bash script, regex, sed, sed bash, shell script



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sed substitution gives error "sed: command garbled" gsai0205 Linux - Newbie 3 09-13-2013 09:01 AM
[SOLVED] sed gives :sed: -e expression #1, char 1: unknown command: `'' samasat Linux - Newbie 10 06-09-2012 05:31 PM
[SOLVED] sed help to run sed command against multiple different file names bkone Programming 2 04-16-2012 12:27 PM
sed - use sed to replace multiple instances from "x" to "y" in a line mrodmac Linux - General 4 02-02-2010 11:37 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration