0

I am having trouble understanding the command below. I understand the argument -i means 'insert' and the last argument obviously references the file to be altered but could someone explain the second argument - s/ quiet init=.*$//

sed -i 's/ quiet init=.*$//' /boot/cmdline.txt

The file /boot/cmdline.txt looks like this:

dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet  init=/usr/lib/raspi-config/init_resize.sh
1
  • The effect of this command is to change the above text to: dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
    – Ctrp
    Aug 7, 2016 at 17:32

2 Answers 2

3

-i for GNU's sed actually means to modify the file in-place, not "insert"

the rest is the sed script s means we're going to do a substitution, then the first / marks the start of the regex until the next / so in this case the pattern is quiet init=.*$ which will find the string quiet init= and everything from there until the end of the line. The next part is what to substitute for the matched expression, in our case it is an empty string, so we effectively delete that part of the line

2
  • Great answer - if I wanted to replace the string with another for example how would I do this?
    – Ctrp
    Aug 7, 2016 at 17:37
  • You put the replacement string between the second and third / characters Aug 7, 2016 at 17:38
1

It seems you've confused sed command line options with sed commands themselves.

An option modifies the default behavior of sed and sed command(s) determines the manipulation that you wish to do with sed.

-i in your case is inplace edit which means make the changes permanently written to the file in question, so you're changing the file

Dont confuse this with the i command which is used to insert a line at a desired place. For example

sed -i '1,i\header' file # i inside single quotes is command

adds the line header to the first line of the file.

The s in your case is the substitution command, the general format for which is

s/pattern/substition/flag

See [ sed manual ] for more details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.