sed help needed

Technical issues/questions of an intermediate or advanced nature.
User avatar
Ed_P
Contributor
Contributor
Posts: 8368
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 5.01 ISO
Location: Western NY, USA

sed help needed

Post#1 by Ed_P » 13 Nov 2020, 06:56

I have a script that references a file in a directory whose name includes a space. I'm trying to edit the directory's name to replace the space with a ? and am failing to achieve that. Help!

Code: Select all

if [ `whoami` != "root" ]; then
  echo "Enter root's password" $(tput bold)$(tput setaf 6)  # 1-red, 2-green, 6-cyan, 7-white
  echo "$0" 
  SH=`echo $0 | sed  's/My /My?/'`
  echo $SH
which results in

Code: Select all

Enter root's password 
/mnt/sda6/Users/Ed/My Utilities/SecretX/UploadX.sh
/mnt/sda6/Users/Ed/My Utilities/SecretX/UploadX.sh
Password: 
Why is my sed failing?? :wall:
Ed

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

sed help needed

Post#2 by Rava » 13 Nov 2020, 15:07

I presume ? is one of the usual regex sed uses, like "." often being a regex, and not a literal . - see my [ code] listing below.

Some regex are harder to work with. Without looking anything up I cannot give you a tip that would fix your issue. I can only presume the ? is such a "more troublesome regex to sed" and that is what causes the issue. I know you simple tried to escape the special character "?" but at times that is not such an easy solution.


What exactly are you trying to accomplish?

Please explain by creating a fake output via a [ code] segment.

And as a gift, have some of the more easy regex'es out there to illustrate how complicated it might get quite easily:

Code: Select all

Character classes
.		any character except newline
\w \d \s	word, digit, whitespace
\W \D \S	not word, digit, whitespace
[abc]		any of a, b, or c
[^abc]		not a, b, or c
[a-g]		character between range from a to g

Anchors
^abc$		start / end of the string
\b \B		word, not-word boundary

Escaped characters
\. \* \\	escaped special characters
\t \n \r	tab, linefeed, carriage return
\u00A9		unicode escaped (c)

Groups & Lookaround
(abc)		capture group
\1		backreference to group #1
(?:abc)		non-capturing group
(?=abc)		positive lookahead
(?!abc)		negative lookahead

Quantifiers & Alternation
a* a+ a?	0 or more, 1 or more, 0 or 1
a{5} a{2,}	exactly five, two or more
a{1,3}  	between one & three
a+? a{2,}?	match as few as possible
ab|cd		match ab or cd
HTH at least a bit. :)
Last edited by Rava on 13 Nov 2020, 15:10, edited 1 time in total.
Reason: expansion
Cheers!
Yours Rava

User avatar
Ed_P
Contributor
Contributor
Posts: 8368
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 5.01 ISO
Location: Western NY, USA

sed help needed

Post#3 by Ed_P » 13 Nov 2020, 15:43

Rava wrote:
13 Nov 2020, 15:07
Please explain by creating a fake output via a [ code] segment.
I thought my code boxes in the OP did that.

"*" allows for any number of characters. "?" allows for any character in just a single position.

For example, searching for Porteus-*-v5.0rc2-x86_64.iso would yield
Porteus-CINNAMON-v5.0rc2-x86_64.iso and
Porteus-LXQT-v5.0rc2-x86_64.iso.

Searching for Porteus-CINNAMON-v5.0rc?-x86_64.iso would yield
Porteus-CINNAMON-v5.0rc1-x86_64.iso and
Porteus-CINNAMON-v5.0rc2-x86_64.iso.

su /mnt/sda6/Users/Ed/My Utilities/SecretX/UploadX.sh fails.
su /mnt/sda6/Users/Ed/My?Utilities/SecretX/UploadX.sh works.

And while I can manually change the script to work for this file in this directory I would like make the code to work in other scripts in other My Utilities directories.

The problem clearer now?
Ed

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

sed help needed

Post#4 by Rava » 13 Nov 2020, 16:06

^
Could it be instead of using the {quote} button you copied my text, thus turning my via bb-code wizardry escaped {code} into the bb-code {code} that did mess up your quote. Please do quote my whole post and look what I did at the place of the {code} to see why your post failed, bb-code-syntax-wise.
(In that whole paragraph just now please do mentally exchange { and } with [ and ] , respectively. I am too lazy to insert the bb-code-escape-wizardry into every single one, and when this post would be wrongly quoted like above it would again mess up the bb-code syntax)
I refer to the post prior you correcting the syntax.
Ed_P wrote:
13 Nov 2020, 15:43
"*" allows for any number of characters. "?" allows for any character in just a single position.
In the shell, yes, and in most regex cases as well.
And no, the way the bash or any other shell handles regex is not always the same sed syntax or any other coding syntax handles regex.
Ed_P wrote:
13 Nov 2020, 15:43
su /mnt/sda6/Users/Ed/My Utilities/SecretX/UploadX.sh fails.
su /mnt/sda6/Users/Ed/My?Utilities/SecretX/UploadX.sh works.
su /mnt/sda6/Users/Ed/My\ Utilities/SecretX/UploadX.sh and su "/mnt/sda6/Users/Ed/My Utilities/SecretX/UploadX.sh" should both also work. HTH!
Cheers!
Yours Rava

User avatar
Ed_P
Contributor
Contributor
Posts: 8368
Joined: 06 Feb 2013, 22:12
Distribution: Cinnamon 5.01 ISO
Location: Western NY, USA

sed help needed

Post#5 by Ed_P » 13 Nov 2020, 22:08

Rava wrote:
13 Nov 2020, 16:06
I refer to the post prior you correcting the syntax.
Since I saw and figured out the problem, which was a cool setup, you could have deleted that paragraph. :happy62:
su /mnt/sda6/Users/Ed/My\ Utilities/SecretX/UploadX.sh and su "/mnt/sda6/Users/Ed/My Utilities/SecretX/UploadX.sh" should both also work. HTH!
And yes those options work also. :good: But they don't resolve the sed question. Anyways, the script runs in terminal mode so time to move on. Thanks for looking at it.
Ed

User avatar
Rava
Contributor
Contributor
Posts: 5401
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.01 x86_64 + 4.0 i586
Location: Forests of Germany

sed help needed

Post#6 by Rava » 14 Nov 2020, 01:22

Ed_P wrote:
13 Nov 2020, 22:08
Since I saw and figured out the problem, which was a cool setup, you could have deleted that paragraph. :happy62:
Bah … it took me ages to write it you know…
Ed_P wrote:
13 Nov 2020, 22:08
Thanks for looking at it.
You are welcome.
Cheers!
Yours Rava

Post Reply