I have a file /etc/singularity/singularity.conf which has the following line:
mount dev = no
I want to automatically change no to yes. I can get this line:
grep "mount dev = no" /etc/singularity/singularity.conf
But how can I edit it to be yes in the file?
sed 's/\(mount dev = \)no/\1yes/' /etc/singularity/singularity.conf
The command above just writes the output to the console. If you want the changes to be applied to the file, you can pass the -i flag, i.e. sed -i ...
You could do this using sed matching the line pattern:
sed '/mount dev =/s/no/yes/' /etc/singularity/singularity.conf
Make sure to add the -i parameter to actually overwrite the file.
Related
in my script i edit a file by using "sed":
sed -i -e /NumberOfEntries=*/d "$playlist_name" > /dev/null # deletes "NumberOfEntries"-line
sed -i -e '/^$/d' "$playlist_name" > /dev/null # deletes all empty lines
This works just fine but while execution (and shortly after) there are one or two temporary files named something like "sedJjHEt2" (the string after sed changes everytime i run the script). The file is marked with a lock and red X-Symbol.
Is there a way to turn this off or am I missing something that I need to adjust? Or is this because of the "-i" Option?
Thanks in advance!
Because of the "-i" Option.
once you use "-i", sed will create a temporary, and then replace the original file with the temporary file.
I am developing a deployment script "that I want to be able to run over again without double entries"
I am trying to add a sed command that will look for "address" field, if it doesn't exist, create it, if it does exist modify it to the correct IP Address.
This is what I have so far...
#!/bin/bash
ipaddress=192.168.1.1
sudo grep -q '^address' /etc/network/interfaces && sudo sed -i 's/^address.*/"address $ipaddress"/' /etc/network/interfaces || echo "address ${ipaddress}" >> /etc/network/interfaces
It will create the correct entry if no entry exists but I have all kinds of problems if the entry exists or is correct.
Any help would be greatly appreciated!
Final Answer based on response from "1stSi Dave" Below
The final working script that creates the entry if it doesn't exist or alters any existing address entry is:
sudo grep -q '^address' /etc/network/interfaces && sudo sed -i -e 's/^address.*/address '$ipaddress'/' /etc/network/interfaces || echo "address ${ipaddress}" >> /etc/network/interfaces
First, I think you need the '-e' command line option to your sed command:
sed -i -e 's/...'
Maybe that's a typo, because the rest of your command line indicates you want to append to the file, not edit in place. Next, single quotes are hiding the variable expansion you're trying to achieve in the sed command script. They are also preserving the double quotes in the output, which I don't think is what you want. Try this:
sed -e 's/^address.*/address '$ipaddress'/' /etc/network/interfaces
Third, you may want to include the possibility of white space preceding the "address" token. Finally, you probably do want to edit-in-place (with sed -i), because tacking on the edited line at the end of the file is probably not going to work.
I would like to add the text foo to the start of all files in a certain directory. I tried the following command:
sed '1i foo' *
But that only added the text to the first file. How can I append to all files in a single sed command? (I know that it can be done with a for loop, I'm specifically asking for a single sed command)
Use option --separate (or short: -s) to consider files as separate rather than as a single continuous long stream.
If you want to alter the files, just add the -i flag:
sed -i '1i foo' *
It'll add foo at the beginning of every file.
Note: without -i, the files weren't actually modified. foo was added at the beginning of the whole content to the standard output only, thus appearing only once.
How about:
sed -i '1 s/^/foo/g' *
For some mysterious reason, some elements in my CSV data appear as s/stWgvN52??f2& ?" instead of stWgvN522tw0JtZZnyXj, which messes up the file because I have ; set as the CSV delimiter.
I attempted to replace the defective string using sed as follows:
$ sed -i 's/stWgvN52??f2& ?"/stWgvN522tw0JtZZnyXj/g' file.csv
but I get the following error:
sed: 1: "access_logs_2014-04.csv": command a expects \ followed by text
What is the reason?
When you use the -i option, you have to specify the extension of the backup file that gets made. Some versions of sed expect the extension directly appended to the -i option, so what you wrote would work. But other versions (like the version on OS X) require it to be a separate option, so you have to write:
sed -i '' 's/stWgvN52??f2& ?"/stWgvN522tw0JtZZnyXj/g' file.csv
to specify that you don't want a backup file.
#!/bin/sed -f
s/","/|/g; # global change of "," to bar
# do some more stuff
#s/|/","/g; # global change of bar back to ","
#---end of script---
The above script removes the 2nd field from a CSV, and clears out quotes and such. I didn't include most of the script because it's not pertinent to the question.
The script is saved in the file fix.sh.
I can run it on a file like this:
$ ./fix.sh <myfile.txt >outputfile.txt
And it works great.
But I want it to replace in file. This doesn't work:
$ ./fix.sh <myfile.txt >myfile.txt
It results in an empty myfile.txt.
This doesn't work either:
$ ./fix.sh myfile.txt
I tried finding some documentation on sed bash scripts but didn't find anything to help me.
I'm sure the answer is simple, I just can't find it. Thanks for your help.
EDIT: I should have mentioned that this is running on a CentOS 6 machine.
Full script is below. Its overall result is to remove field#2 and strip quotes.
#!/bin/sed -nf
# adapted from http://www.linuxtopia.org/online_books/linux_tool_guides/the_sed_faq/sedfaq4_005.html
s/","/|/g; # global change of "," to bar
s/^"//;
s/"$//;
s/^\([^|]*\)|[^|]*|/\1|/; # delete 2nd field contents
s/||/|/; # change || to |
s/ //g; # remove spaces
s/|/,/g;
#s/|/","/g; # global change of bar back to ","
#---end of script---
If your sed supports -i option then you can run your script like this:
./fix.sh -i myfile.txt
-i option of sed does the in-file substitutions.
If your version of sed does not support the -i option then you can do the following which is pretty much the same thing that -i does behind the scene:
./fix.sh myfile.txt > temp && mv temp myfile.txt
Why redirecting to the same file doesn't work?
The reason is that the redirection opens the file for writing and ends up clearing any existing contents. sed then tries to read this empty file, and does nothing. The file is then closed and there by you get an empty file.