You are not logged in.

#1 2007-12-21 18:22:29

rasat
Forum Fellow
From: Finland, working in Romania
Registered: 2002-12-27
Posts: 2,293
Website

Can sed handle this type of task.

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

#2 2007-12-21 19:01:44

.:B:.
Forum Fellow
Registered: 2006-11-26
Posts: 5,819
Website

Re: Can sed handle this type of task.

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

#3 2007-12-21 19:15:26

schivmeister
Developer/TU
From: Singapore
Registered: 2007-05-17
Posts: 971
Website

Re: Can sed handle this type of task.

Pretty interesting these unix tools and commands. For example, sed also works like this: sed -i "s:$var1:$var2:g" and "s_$var1_$var2_" lol

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

#4 2007-12-22 12:12:37

rasat
Forum Fellow
From: Finland, working in Romania
Registered: 2002-12-27
Posts: 2,293
Website

Re: Can sed handle this type of task.

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

#5 2007-12-23 01:10:32

ibendiben
Member
Registered: 2007-10-10
Posts: 519

Re: Can sed handle this type of task.

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 cool

#!/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 wink

Offline

#6 2007-12-23 05:37:31

peets
Member
From: Montreal
Registered: 2007-01-11
Posts: 936
Website

Re: Can sed handle this type of task.

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

#7 2007-12-23 21:44:29

rasat
Forum Fellow
From: Finland, working in Romania
Registered: 2002-12-27
Posts: 2,293
Website

Re: Can sed handle this type of task.

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

Board footer

Powered by FluxBB