Page 1 of 1

Sed command not working

Posted: Sun Apr 07, 2019 5:57 pm
by Void Frost
Hello! I'm trying to make an easier way to overclock a Pi, and i'm trying to echo the new arm_freq=1500 to the config.txt on line 78 but for some reason it won't copy over. My command is as follows:

Code: Select all

sudo sed -i 78i\arm_freq=1500 /boot/config.txt

Re: Sed command not working

Posted: Sun Apr 07, 2019 6:17 pm
by tpyo kingg
There are not normally 78 lines in config.txt, at least not by default. So the condition will never be met and the insertion never made. If you would like to append to the file, try something like this to insert or append on the final line:

Code: Select all

sudo sed -i.bak '$i\arm_freq=1500' /boot/config.txt

# or

sudo sed -i.bak '$a\arm_freq=1500' /boot/config.txt
The $ stands for the end of the file.

Re: Sed command not working

Posted: Sun Apr 07, 2019 6:29 pm
by jahboater
Or just:

echo "arm_freq=1500" >>/boot/config.txt

Re: Sed command not working

Posted: Sun Apr 07, 2019 7:59 pm
by rpdom
jahboater wrote:
Sun Apr 07, 2019 6:29 pm
Or just:

echo "arm_freq=1500" >>/boot/config.txt
Which will fail because of permissions. The sudo is required.

You'd need to use something like

Code: Select all

sudo sh -c "echo 'arm_freq=1500' >>/boot/config.txt"

Re: Sed command not working

Posted: Sun Apr 07, 2019 10:33 pm
by Void Frost
Is there a way I can easily add lines?

Re: Sed command not working

Posted: Mon Apr 08, 2019 12:25 am
by echmain
Void Frost wrote:
Sun Apr 07, 2019 10:33 pm
Is there a way I can easily add lines?

Any reason you can't use an editor? Vi or Nano or whatever?

Re: Sed command not working

Posted: Mon Apr 08, 2019 4:56 am
by tpyo kingg
Void Frost wrote:
Sun Apr 07, 2019 10:33 pm
Is there a way I can easily add lines?
Yes, you can append using either sed or as shell redirection. Both methods are shown above. An advantage of using sed with -i is that it can create a backup file. The advantage of the appending redirection >> is that it is simple.

Can you please say a little about your task? Depending on the context there might be something else important.

Re: Sed command not working

Posted: Mon Apr 08, 2019 2:03 pm
by jahboater
tpyo kingg wrote:
Mon Apr 08, 2019 4:56 am
Void Frost wrote:
Sun Apr 07, 2019 10:33 pm
Is there a way I can easily add lines?
Yes, you can append using either sed or as shell redirection. Both methods are shown above. An advantage of using sed with -i is that it can create a backup file. The advantage of the appending redirection >> is that it is simple.
If adding multiple lines from the shell, its common to use something called a "here document":-

Code: Select all

cat <<end >>/boot/config.txt
line 1
line 2
line 3
end
As noted above, you need root to write to /boot