Hello,
In bash, I am looking for a “search line and replace” function.
For example, in /etc/X11/xorg.conf, automatically change the line :
Driver "nvidia"
by :
Driver "nouveau"
without copying the whole file. Does it exist ?
Hello,
In bash, I am looking for a “search line and replace” function.
For example, in /etc/X11/xorg.conf, automatically change the line :
Driver "nvidia"
by :
Driver "nouveau"
without copying the whole file. Does it exist ?
And there are tons of sed or awk scripts on Google, but I am looking for a standalone bash command, well tested, with man help…
On 08/29/2012 12:06 PM, dsant fr wrote:
> And there are tons of sed creations on Google, but I look for a
> standalone bash command, with man…
since sed is so available in every bash i ever saw i can’t imagine why
you want to avoid it, but have you tried to exclude sed in your search,
like:
https://www.google.com/search?q=bash+command+line+search+and+replace+-sed
–
dd
sed -i '/Driver/s/nvidia/nouveau/' /etc/X11/xorg.conf
I hope it’s just an example, because switching the graphics driver would require a little bit more work.
No there isn’t in *bash. *This is not the task of a shell. A shell enables you to call all sorts of tools. You could write a script yourself in bash, but who would do that since sed is allready available for forty years?
And even when you write it yourself, you need an intermediate file. Reading and writing to the same file at the same moment will lead to all sorts of merry entertainment. :sarcastic:
sed --in-place (sed -i) is OK though.
For those who think that sed -i will brake the laws of computing ;):
-i[SUFFIX]'
–in-place=SUFFIX]’
This option specifies that files are to be edited in-place. GNU
`sed’ does this by creating a temporary file and sending output to
this file rather than to the standard output.(1).
Thus it will organise the temporary file for you and it will copy the whole file. That is not what the OP asked for (maybe he thinks the file copying is taking to much resources).
Of course it does! But the user doesn’t see it and doesn’t care. And unless I misunderstood the first post, that’s exactly what the OP asked for:
which probably means “I would like not to have to copy the file myself”.
I use “sed -i” a lot because it is shorter (I like short code). Thus instead of this:
cp /etc/X11/xorg.conf{,.old} && sed '/Driver/s/nvidia/nouveau/' /etc/X11/xorg.conf.old > /etc/X11/xorg.conf
I use this:
sed **-i** '/Driver/s/nvidia/nouveau/' /etc/X11/xorg.conf
Notice that you can make a backup (more exactly keep sed’s temporary backup) if you like:
sed **-i.bak** '/Driver/s/nvidia/nouveau/' /etc/X11/xorg.conf
Without the option “-i”, any attempt to read/write the same file with sed will result in an empty file, which is of course not desired.
DO NOT use this:
sed '/Driver/s/nvidia/nouveau/' /etc/X11/xorg.conf > /etc/X11/xorg.conf
or this:
cat /etc/X11/xorg.conf | sed '/Driver/s/nvidia/nouveau/' > /etc/X11/xorg.conf
But it has nothing to do with bash. sed is just an external program that you can use in bash or in any other shell (sh, csh, etc). Although sed has a paramater expansion which can replace an expression in a variable, but using it to replace a word in a file would be insane … but possible.
Of course it does! But the user doesn’t see it and doesn’t care. And unless I misunderstood the first post, that’s exactly what the OP asked for
He did not say that his reason to be against file copy (most people thinh that their strain of thought is the only logical one :(). And though you might be correct in assuming that he thinks that having an “in bwteen” file makes things to complicated, it is not for sure. He could have files of a lot of GB where he wants to avoid the extra I/O and/or doesn’t have the spare space for the second file.
Wanna split hairs?
Nothing is for sure. But if one wonder if a program does internally make a copy of a file before modifying it, he would either know the answer or formulate the question differently. But who knows what people have in mind? …