[SOLVED] sed simple syntax question

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
User avatar
majpooper
Level 8
Level 8
Posts: 2087
Joined: Thu May 09, 2013 1:56 pm
Location: North Carolina, USA

[SOLVED] sed simple syntax question

Post by majpooper »

I have taken several of the tweaks recommended by Pjotr at his web site and put them in a script. The line below works fine, however for readability I would rather it be split into two or three lines. I have researched sed a bit and have tried a million way using the back slash but still cannot split the line and get it to work. It will work the way it is but I would rather it be in more than one line in the script. :?

Code: Select all

sudo sed -i 's/GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"/GRUB_CMDLINE_LINUX_DEFAULT="quiet splash zswap.enabled=1 zswap.zpool=z3fold zswap.compressor=lz4"/' /etc/default/grub
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
AwesomeOpossum74
Level 3
Level 3
Posts: 118
Joined: Fri Jan 21, 2022 2:29 pm

Re: sed simple syntax question

Post by AwesomeOpossum74 »

How about:

Code: Select all

sudo sed -i s/\
'GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"'/\
'GRUB_CMDLINE_LINUX_DEFAULT="quiet splash zswap.enabled=1 zswap.zpool=z3fold zswap.compressor=lz4"'/\
 /etc/default/grub
I recommend you verify it works on a test file before running it on your working grub file.
User avatar
majpooper
Level 8
Level 8
Posts: 2087
Joined: Thu May 09, 2013 1:56 pm
Location: North Carolina, USA

Re: sed simple syntax question

Post by majpooper »

AwesomeOpossum74 wrote: Mon Jan 24, 2022 9:34 pm How about:

Code: Select all

sudo sed -i s/\
'GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"'/\
'GRUB_CMDLINE_LINUX_DEFAULT="quiet splash zswap.enabled=1 zswap.zpool=z3fold zswap.compressor=lz4"'/\
 /etc/default/grub
I recommend you verify it works on a test file before running it on your working grub file.
THX much - but one thing I am confused about is:
THIS: the " and the ' inside the / this works for multi line

Code: Select all

compressor=lz4"'/\
AND THIS: The " inside the / and the ' outside this works for a single line

Code: Select all

compressor=lz4"/' /etc/default/grub
The syntax does not seem intuitive I think I get the purpose of the double quotes, but not the single quotes.
I appreciate the help. I have some work to do to try an understand the sed syntax better.
AwesomeOpossum74
Level 3
Level 3
Posts: 118
Joined: Fri Jan 21, 2022 2:29 pm

Re: [SOLVED] sed simple syntax question

Post by AwesomeOpossum74 »

Normally things inside double quotes would be interpreted, like if you had a $variable. Stuff inside single quotes don't get interpreted, and are passed as is. Since the double quotes are inside the single quotes, the single quotes take precedence, and the double quotes will not be interpreted. Probably a long-winded and confusing explanation. Sorry about that.

Not actually testing this myself, but since the sed delimiter is slash (/), you could probably dispense with the single quotes, unless you later add slashes to the values.

Edit: How about this explanation: The single quotes force the double quotes to be interpreted as part of the string, rather than special characters to be functionally interpreted.
User avatar
majpooper
Level 8
Level 8
Posts: 2087
Joined: Thu May 09, 2013 1:56 pm
Location: North Carolina, USA

Re: [SOLVED] sed simple syntax question

Post by majpooper »

AwesomeOpossum74 wrote: Mon Jan 24, 2022 10:45 pm Normally things inside double quotes would be interpreted, like if you had a $variable. Stuff inside single quotes don't get interpreted, and are passed as is. Since the double quotes are inside the single quotes, the single quotes take precedence, and the double quotes will not be interpreted. Probably a long-winded and confusing explanation. Sorry about that.

Not actually testing this myself, but since the sed delimiter is slash (/), you could probably dispense with the single quotes, unless you later add slashes to the values.

Edit: How about this explanation: The single quotes force the double quotes to be interpreted as part of the string, rather than special characters to be functionally interpreted.
THX - appreciate the explanation - not long winded at all and helpful
LittleScriptMan
Level 3
Level 3
Posts: 158
Joined: Thu Jan 13, 2022 8:42 am

Re: [SOLVED] sed simple syntax question

Post by LittleScriptMan »

AwesomeOpossum74 wrote: Mon Jan 24, 2022 10:45 pm since the sed delimiter is slash (/)
That is not mandatory. In this topic viewtopic.php?f=213&t=365559, I submitted this command :

Code: Select all

find $CustDir -name '*.cust' -exec sed -i 's|home/'$PrevUser'|home/'$USER'|g' {} +
where the delimiter was the pipe.

In his valuable help answer, rene answered with :
Using s,,,g rather than your s|||g only as a (strong) personal preference...

Code: Select all

sed -i "s,/home/[^/]\+/,/home/$USER/,g" "$CustDir"/*.cust
using the comma as a delimiter.

So, you can use other delimiters when it helps being more readable (the pipe and the comma prevented confusion with the delimiter slash and the slashes included in paths). Of course, you can keep on using slashes even when dealing with paths if the command is set to take the difference in account.
Interests : Firefox, Cinnamon & Bash Scripts
LM Version : LMDE5 (LMDE4 just in case)
User avatar
majpooper
Level 8
Level 8
Posts: 2087
Joined: Thu May 09, 2013 1:56 pm
Location: North Carolina, USA

Re: [SOLVED] sed simple syntax question

Post by majpooper »

LittleScriptMan wrote: Tue Jan 25, 2022 7:48 am So, you can use other delimiters when it helps being more readable (the pipe and the comma prevented confusion with the delimiter slash and the slashes included in paths). Of course, you can keep on using slashes even when dealing with paths if the command is set to take the difference in account.
THX - I think I am beginning to catch on . . . . albeit slowly.
I have been looking over a sed tutorial online - it shows basics commands and uses delimiters but really does not explain them in terms hierarchy if there is one . . . . or do you just use them interchangeably to suit one's preference and readability. I guess I will just have to experiment.

BTW - when I started delving into java script about a year ago I haven't learned much but I sure have broken a lot of stuff. Good thing I was a Business major (Industrial Relations) and not Computer Science, I would have never made it past my Freshman year.
LittleScriptMan
Level 3
Level 3
Posts: 158
Joined: Thu Jan 13, 2022 8:42 am

Re: [SOLVED] sed simple syntax question

Post by LittleScriptMan »

majpooper wrote: Tue Jan 25, 2022 2:02 pm I have been looking over a sed tutorial online - it shows basics commands and uses delimiters but really does not explain them in terms hierarchy if there is one . . . . or do you just use them interchangeably to suit one's preference and readability. I guess I will just have to experiment.
There is no hierarchy. You can use any except the backslash and the newline, even A,z, 4, £. Of course, the goal should still be better readability. Probably special characters with accents (french), tildes (spanish) or diaresis (swedish) would be problematic too. Never tried. Either the usual / or the pipe when already using slashes in the search or replace expressions worked fine for me, even if the comma may seem more different from the slash than the pipe with is somewhat a vertical slash.

About tutorials, I work on the Baeldung site. Here are their links about sed hoping they may provide some help :
https://www.baeldung.com/linux/grep-sed-awk-differences
https://www.baeldung.com/linux/sed-editor
https://www.baeldung.com/linux/sed-subs ... -variables
https://www.baeldung.com/linux/sed-with-string
https://www.baeldung.com/linux/sed-repl ... ine-string
https://www.baeldung.com/linux/sed-awk-return-value
Interests : Firefox, Cinnamon & Bash Scripts
LM Version : LMDE5 (LMDE4 just in case)
Locked

Return to “Scripts & Bash”