You are not logged in.
Can sed handle this type of task, or any other stream editor?
/tmp/hwd.tmp
Section "InputDevice"
Identifier "USB Mouse"
Driver "mouse"
Option "Device" "/dev/input/mice"
Option "SendCoreEvents" "true"
Option "Protocol" "IMPS/2"
Option "ZAxisMapping" "4 5"
Option "Buttons" "5"
EndSection
OrgTEXT=`cat /tmp/hwd.tmp`
EditTEXT=`cat /tmp/xedit.tmp`
cat /etc/X11/xorg.conf.vesa | sed "s/$OrgTEXT/$EditTEXT/g" >/etc/X11/xorg.conf
Markku
Offline
Sed can replace text in an existing file with the -i option, so no redirection needed. Would that be a bit closer to what you look for?
E.g.
var1=bla
var2=dibla
sed -i "s|$var1|$var2|g" /etc/X11/xorg.conf
I don't know if using whole batches of text will work though, although I suppose it should. Looks a bit more complicated.
Last edited by B (2007-12-21 19:03:29)
Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy
Offline
Pretty interesting these unix tools and commands. For example, sed also works like this: sed -i "s:$var1:$var2:g" and "s_$var1_$var2_"
Yes, whole batches of text can be used since everything is based on patterns. If the pattern matches, sed will do it.
I need real, proper pen and paper for this.
Offline
Doesn't work when dealing with multiple lines.
var1="bla
blaa"
var2="dibla
diblazzz"
sed -i "s|$var1|$var2|g" /etc/X11/xorg.conf
Markku
Offline
I guess there should be some simpler way but you could use this script to get things done like you want (as I think you want), and it uses sed to do the job
#!/bin/bash
#
#A very complicated solution would look like this:
#
#Example:
#
echo "hello
hello" >/tmp/pattern
echo "hola
hola" >/tmp/replacement
echo "hello
this is is a test
hello
hello
above hello's should look like this shortly
hola
hola" >/tmp/yourfile
#<sed -e :a -e '$!N;/\n/s//@@/;ta' -e 'P;D'>
#replaces newlines (\n) with "@@"
cat /tmp/yourfile
echo "-------------------------------"
#actual codes needed:
VAR1=`cat /tmp/pattern | sed -e :a -e '$!N;/\n/s//@@/;ta' -e 'P;D'`
VAR2=`cat /tmp/replacement | sed -e :a -e '$!N;/\n/s//@@/;ta' -e 'P;D'`
cat /tmp/yourfile | sed -e :a -e '$!N;/\n/s//@@/;ta' -e 'P;D' | sed "s/$VAR1/$VAR2/g" | sed "s/@@/\n/g" >/tmp/patchedfile
#end of example
cat /tmp/patchedfile
rm -f /tmp/pattern /tmp/replacement /tmp/yourfile /tmp/patchedfile
I just created a example out of it so you could see it in action... It's all about the idea ofcourse
Offline
A solution can be found if you read this: http://www.grymoire.com/Unix/Sed.html#uh-56 and this: http://www.grymoire.com/Unix/Sed.html#uh-58 (and maybe the section in between the two)
In short, 'H' will add the line to the "hold buffer". 'x' will replace the pattern space with the hold buffer. '{' and '}' allow you to group commands.
So I think you can do something like this:
sed -n 'H
$ {
x
s/$OrgText/EditText/p
}
'
I'm at my parents' place for the holidays, and they only run MS Windows (for now...) so I can't test this. I might not have understood the grouping properly.
Last edited by peets (2007-12-24 04:26:04)
Offline
Thanks for the solutions. The solution of ibendiben works fine with an additional syntax (sed 's/\//|/g'). I was not able to fix peets.
cat $ConfigFILE | sed 's/\//|/g' >/tmp/xorg.tmp
OrgTEXT=`cat /tmp/hwd.tmp | sed -e :a -e '$!N;/\n/s//@@/;ta' -e 'P;D' | sed 's/\//|/g'`
EditTEXT=`cat /tmp/xedit.tmp | sed -e :a -e '$!N;/\n/s//@@/;ta' -e 'P;D' | sed 's/\//|/g'`
cat /tmp/xorg.tmp | sed -e :a -e '$!N;/\n/s//@@/;ta' -e 'P;D' | sed "s/$OrgTEXT/$EditTEXT/g" | sed "s/@@/\n/g" | sed 's/|/\//g' >$ConfigFILE
Markku
Offline