Comments in sed command file - linux

Can I put comments (or something functionally equivalent) into a sed command file?
subs.sed
s/this/that/g
# comment
s/it/they/g
$ sed -i -f subs.sed <(echo this it)
that they

Yes, comments can be added to a sed file using #.
From the manual page of sed:
Command Synopsis
...
#comment
The comment extends until the next newline (or the end of a -e script fragment).

Related

Add a kernel parameter with sed to grub

I'm writing a bash script to non-interactively enable hibernation on a linux system. To this end, I need to insert a shell variable that contains slashes on a specific line of a while, inside quotes that are on that line.
The relevant part of the file to b edited looks like this:
GRUB_DEFAULT=0
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="Manjaro"
GRUB_CMDLINE_LINUX_DEFAULT="quiet"
GRUB_CMDLINE_LINUX=""
I need to change it to this:
GRUB_DEFAULT=0
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="Manjaro"
GRUB_CMDLINE_LINUX_DEFAULT="quiet resume=/dev/disk/by-partuuid/c5a552c2-fe8f-423a-9037-c35bf090d9c3"
GRUB_CMDLINE_LINUX=""
The added parameter is provided by a shell variable.
I tried this:
sed -i '\*^GRUB_CMDLINE_LINUX_DEFAULT* s*"$* '"$(grub_resume_boot_option)"'"*' /etc/default/grub
Logic that I was aiming for was: "Look for a line that starts with pattern "GRUB_CMDLINE_LINUX_DEFAULT", and replace the last " with the given pattern. Use * as delimiter to preserve the slashes in the expanded variable."
However, the command fails if there are any spaces at the end of the line. Is there any way to make it not take spaces at the end of the line into account?
Also, if there is more simple or readable solution to this, I would be very interested.
You can do:
sed -i 's#^\(GRUB_CMDLINE_LINUX_DEFAULT="quiet\)"$#\1 resume=/dev/disk/by-partuuid/c5a552c2-fe8f-423a-9037-c35bf090d9c3"#' /etc/default/grub
Example:
$ sed 's#^\(GRUB_CMDLINE_LINUX_DEFAULT="quiet\)"$#\1 resume=/dev/disk/by-partuuid/c5a552c2-fe8f-423a-9037-c35bf090d9c3"#' <<<'GRUB_CMDLINE_LINUX_DEFAULT="quiet"'
GRUB_CMDLINE_LINUX_DEFAULT="quiet resume=/dev/disk/by-partuuid/c5a552c2-fe8f-423a-9037-c35bf090d9c3"
From: https://serverfault.com/questions/885684/editing-the-value-of-grub-cmdline-linux-default-thru-bash-script
sed -i 's/^GRUB_CMDLINE_LINUX_DEFAULT="/&newstuff /' /etc/default/grub
This will add "newstuff" to the beginning of the list of params. Examples:
sed 's/^GRUB_CMDLINE_LINUX_DEFAULT="/&newstuff /' <<< 'GRUB_CMDLINE_LINUX_DEFAULT="quiet"'
GRUB_CMDLINE_LINUX_DEFAULT="newstuff quiet"
sed 's/^GRUB_CMDLINE_LINUX_DEFAULT="/&newstuff /' <<< 'GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"'
GRUB_CMDLINE_LINUX_DEFAULT="newstuff quiet splash"
sed -i "s/GRUB_CMDLINE_LINUX_DEFAULT="[^"]*/& $(grub_resume_boot_option)/" /etc/default/grub

One line bash Script to Edit File (not create a new one)

I'm a newbie at bash script and I'm trying to write a bash script to edit a configuration file. i want to remove " foo" from the file.
Using the code
echo
sed -e "s/ foo//" ./test > ./test2
will create a new file with the content i want. But if i try to edit the same file using this
echo
sed -e "s/ foo//" ./test > ./test
the file will just become empty. Is this not the way? how can i simply edit a file? thanks.
This is what the -i switch is for: edit-in-place:
sed -i -e "s/ foo//" ./test
The -i switch is documented in man sed:
-i[SUFFIX], --in-place[=SUFFIX]
edit files in place (makes backup if extension supplied)
For GNU sed (linux), no back-up is created when the suffix is omitted. For other versions of sed, it might be mandatory to supply a backup suffix.
Empty file
Shell opens the file test for writing -> clearing all its previous contents.
How to edit same file
Solution 1:- inline change
sed -i.bk "s/ foo//" ./test
Solution 2:- use intermediate file (manual version of previous cmd)
sed -e "s/ foo//" ./test > test.bk && mv test.bk test
Sed is a [s]tream [ed]itor, not a text editor. Using the '-i' parameter is not POSIX, plus it adds the overhead of creating a temporary file before replacing the original file.
If you want to edit your files truly in place, you should use ed/ex instead.
ex -sc '%s/ foo//g|xit' ./test
See the manual pages sed(1p), ed(1p) and ex(1p).

Bash: Update a variable within a file

I know this is a simple answer and I could probably keep digging around on Google before I stroll across the answer. But I am on a tight schedule and I was hoping for an easy response.
I need to update a variable in ifcfg-eth0 upon an installation. So in other words, this is what needs to happen:
The following variables need to change from:
ONBOOT=no
BOOTPROTO=dhcp
to
ONBOOT=yes
BOOTPROTO=static
Thanks in advance!
Cheers.
sed -i -e '/^ONBOOT=/s|.*|ONBOOT=yes|; /^BOOTPROTO=/s|.*|BOOTPROTO=static|' file
Also try:
sed -i -re 's|^(ONBOOT=).*|\1yes|; s|^(BOOTPROTO=).*|\1static|' file
Or
sed -i -e 's|^\(ONBOOT=\).*|\1yes|; s|^\(BOOTPROTO=\).*|\1static|' file
You can make use of something like this, which lets you define exactly what values you want to add in the file:
$ bootproto="static"
$ sed -r "s/(BOOTPROTO\s*=\s*).*/\1$bootproto/" file
ONBOOT=no
BOOTPROTO=static
And to make the two of them together:
$ onboot="yes"
$ bootproto="static"
$ sed -r -e "s/(ONBOOT\s*=\s*).*/\1$onboot/" -e "s/(BOOTPROTO\s*=\s*).*/\1$bootproto/" file
ONBOOT=yes
BOOTPROTO=static
Explanation
(BOOTPROTO\s*=\s*).* catches a group of text containing BOOTPROTO + any_number_of_spaces + = + any_number_of_spaces. Then, matches the rest of the text, that we want to remove. \1$var prints it back together with the given variable.
\s* keeps the current spaces as they were and then replaces the same line changing the current text with the bash variable $bootproto.
-r is used to catch groups with () instead of \( and \).
To make it edit in place, use sed -i.bak. This will create a backup of the file, file.bak, and the file will be updated with the new content.
All together:
sed -ir -e "s/(ONBOOT\s*=\s*).*/\1$onboot/" -e "s/(BOOTPROTO\s*=\s*).*/\1$bootproto/" file
The similar as the sed solutons with perl
perl -i.bak -pe 's/(ONBOOT)=no/$1=yes/;s/(BOOTPROTO)=dhcp/$1=static/' files....

sed -e 's/^M/d' not working

A very common problem, but I am unable to work around it with sed.
I have a script file ( a batch of commands) say myfile.txt to be executed at once to create a list. Now when I am executing a batch operation my command line interface clearly shows its unable to parse the command as a line feed ^M is adding up at end of each line.
I thought sed to be the best way to go about it.I tried:
sed -e 's/^M/d' myfile.txt > myfile1.txt
mv myfile1.txt myfile.txt
It didn't work. I also tried this and it didn't work:
sed -e 's/^M//g' myfile.txt > myfile1.txt
mv myfile1.txt myfile.txt
Then I thought may be sed is taking it as a M character in the beginning of line, and hence no result. So I tried:
sed -e 's/\^M//g' myfile.txt > myfile1.txt
mv myfile1.txt myfile.txt
But no change. Is there a basic mistake I am doing ? Kindly advise as I am bad at sed.
I found a resolution though which was to open the file in vi editor and in command mode execute this:
:set fileformat=unix
:w
But I want it in sed as well.
^M is not literally ^M. Replace ^M with \r. You can use the same representation for tr; these two commands both remove carriage returns:
tr -d '\r' < input.txt > output.txt
sed -e 's/\r//g' input.txt > output.txt
sed -e 's/^M/d' myfile.txt
Has the following meaning [the same for /\^M/ ]: If the first letter of the line is M, then remove the line, else print it and pass to next.. And you have to insert 2 separators /old/new/ in s[earch command].
This may help you.
Late, but here for posterity: sed Delete / Remove ^M Carriage Return (Line Feed / CRLF) on Linux or Unix
The gist, which answers the above question: to get ^M type CTRL+V followed by CTRL+M i.e. don’t just type the carat symbol and a capital M. It will not work

Sed Append Line

Does sed have a command to append a line after a matched line? I tried searching around but was a bit confused with the results.
Basiclly I want it to match
#address=/doubleclick.net/127.0.0.1
And add a line below it like
#address=/doubleclick.net/127.0.0.1
address=/anotherurl/ipaddress
Is this possible?
You can use the a(append) command in sed:
$ sed -i '/^#address=\/doubleclick.net\/127.0.0.1$/a\
> address=/anotherurl/ipaddress' file.txt
Or you can use s(substitute) command:
$ sed -i 's#^#address=/doubleclick.net/127.0.0.1$#&\naddress=/anotherurl/ipaddress#' file.txt
Note: $ and > are bash prompt.
This might work for you:
echo "#address=/doubleclick.net/127.0.0.1"|
sed '\|#address=/doubleclick.net/127.0.0.1|a\address=/anotherurl/ipaddress'
#address=/doubleclick.net/127.0.0.1
address=/anotherurl/ipaddress
You can use any delimiter you like in an address by prepending a \ i.e. \|...| for the substitute command the \ is not necessary.
If you want a blank line then some text following the match, use:
echo "#address=/doubleclick.net/127.0.0.1"|
sed '\|#address=/doubleclick.net/127.0.0.1|a\\naddress=/anotherurl/ipaddress'
#address=/doubleclick.net/127.0.0.1
address=/anotherurl/ipaddress

Resources