iccaros

Sed Help

14 posts in this topic

I have a bash script where I am trying to get this funtion to work

it needs to find the first instance of a line and append a line after it

function writeline()

{

ADDLINE="\<rdp_server client_audio=\"$AUDIO\"\ color_depth=\"$COLOR\"\ comment=\"$COMMENT\"\ experience=\"$EXPERIENCE\"\ gateway=\"$GATEWAY\"\ geometry=\"$GEOMETRY\"\ ip=\"$IP\"\ submenu=\"$SUBMENU\" title=\"$TITLE\"\ \/\>"

eval "sed -e s/^<rdp_server/$ADDLINE/ $INFILE > $TMPFILE"

}

both $INFILE and $TMPFILE are declared the error I get is

./addsub: line 24: rdp_server/<rdp_server: No such file or directory

thanks

Share this post


Link to post
Share on other sites

thanks for the reply but it still fails for me

here is what I get./addsub: line 27: rdp_server/$ADDLINE/: ambiguous redirect

knoppix@Microknoppix:~$ vi addsub

knoppix@Microknoppix:~$ ./addsub

./addsub: line 27: rdp_server/$ADDLINE/: ambiguous redirect

knoppix@Microknoppix:~$ vi addsub

knoppix@Microknoppix:~$ ./addsub

./addsub: line 27: rdp_server/\<rdp_server client_audio="On"\ color_depth="16"\ comment="Created by addsub script"\ experience="0"\ gateway="12.0.0.1"\ geometry="90%"\ ip=""\ submenu="" title=""\ \/\>/: No such file or directory

knoppix@Microknoppix:~$

here is the entire script

#Script Variables, hard coded values are site default
AUDIO="On"
COLOR="16"
COMMENT="Created by addsub script"
EXPERIENCE="0"
GATEWAY="12.0.0.1"
GEOMETRY="90%"
IP=""
SUBMENU=""
TITLE=""
INFILE="./testfile"
TMPFILE="./tempfile"


function writeline()
{
ADDLINE="\<rdp_server client_audio=\"$AUDIO\"\ color_depth=\"$COLOR\"\ comment=\"$COMMENT\"\ experience=\"$EXPERIENCE\"\ gateway=\"$GATEWAY\"\ geometry=\"$GEOMETRY\"\ ip=\"$IP\"\ submenu=\"$SUBMENU\" title=\"$TITLE\"\ \/\>"

sed -e s/^<rdp_server/"$ADDLINE"/ $INFILE > $TMPFILE
}


writeline

Share this post


Link to post
Share on other sites

going to this

ADDLINE='<rdp_server client_audio=\"$AUDIO\" color_depth=\"$COLOR\" comment=\"$COMMENT\" experience=\"$EXPERIENCE\" gateway=\"$GATEWAY\" geometry=\"$GEOMETRY\" ip=\"$IP\" submenu=\"$SUBMENU\" title=\"$TITLE\" />'

sed -e s/^<rdp_server/"$ADDLINE"/ $INFILE > $TMPFILE

gives me

knoppix@Microknoppix:~$ ./addsub

./addsub: line 27: rdp_server/<rdp_server client_audio=\"$AUDIO\" color_depth=\"$COLOR\" comment=\"$COMMENT\" experience=\"$EXPERIENCE\" gateway=\"$GATEWAY\" geometry=\"$GEOMETRY\" ip=\"$IP\" submenu=\"$SUBMENU\" title=\"$TITLE\" />/: No such file or directory

Share this post


Link to post
Share on other sites

really I want to find the first match then add my line, not to everyline that matches

thanks

I have been beeting my head on doing this in bash and python (my only choices besides pearl which I no nothing about)

Share this post


Link to post
Share on other sites

You have to quote the argument to sed. FWIW, I think it would be easier to use ed and a here-doc:

ed $INFILE <<EOL
/^<rdp_server/c
<rdp_server client_audio="$AUDIO" color_depth="$COLOR" comment="$COMMENT"
experience="$EXPERIENCE" gateway="$GATEWAY"
geometry="$GEOMETRY" ip="$IP"
submenu="$SUBMENU" title="$TITLE"/>
.
wq $OUTFILE
EOL

Share this post


Link to post
Share on other sites

hmmm

but if i quote the variable is not passed its taken as a literal value

I am writing this script to let a windows admin add line (questions will fill in the data later) so I am putting it in a script, if it was me I would just open the xml file and yyp the lines and edit them, but its not for me..

I don't have ed but thanks

Share this post


Link to post
Share on other sites
but if i quote the variable is not passed its taken as a literal value

Double-quote.

I don't have ed but thanks

Where did you find a system with bash and sed but not ed? How about ex?

Share this post


Link to post
Share on other sites

ok this is close

<code>

#Script Variables, hard coded values are site default

AUDIO="On"

COLOR="16"

COMMENT="Created by addsub script"

EXPERIENCE="0"

GATEWAY="12.0.0.1"

GEOMETRY="1660x1024"

IP=""

SUBMENU=""

TITLE=""

INFILE="./testfile"

TMPFILE="./tempfile"

function writeline()

{

ADDLINE="\<rdp_server\ client_audio=\"$AUDIO\"\ color_depth=\"$COLOR\"\ comment=\"$COMMENT\"\ experience=\"$EXPERIENCE\"\ gateway=\"$GATEWAY\"\ geometry=\"$GEOMETRY\"\ ip=\"$IP\"\ submenu=\"$SUBMENU\"\ title=\"$TITLE\"\ \/\>"

sed -e "s/^<rdp_server/$ADDLINE/1" $INFILE > $TMPFILE

}

writeline

</code>

but I only want to append the line after the first find ?

thanks

Share this post


Link to post
Share on other sites
Where did you find a system with bash and sed but not ed? How about ex?

Knoppix..

Who knows..

maybe I should use awk...

I am so out of bash scripting I feel dumb..

Share this post


Link to post
Share on other sites

You could use a command block and b in sed but... blech.

Share this post


Link to post
Share on other sites

got it..

{

ADDLINE="\<rdp_server\ client_audio=\"$AUDIO\"\ color_depth=\"$COLOR\"\ comment=\"$COMMENT\"\ experience=\"$EXPERIENCE\"\ gateway=\"$GATEWAY\"\ geometry=\"$GEOMETRY\"\ ip=\"$IP\"\ submenu=\"$SUBMENU\"\ title=\"$TITLE\"\ \/\>"

#sed -e "s/^<rdp_server/$ADDLINE/1" $INFILE > $TMPFILE

sed "0,/^<rdp_server/a$ADDLINE/" $INFILE > $TMPFILE

}

Thanks..

Share this post


Link to post
Share on other sites

ok change but I can noit get it to write to the file

just standard output

ADDLINE="\<rdp_server\ client_audio=\"$AUDIO\"\ color_depth=\"$COLOR\"\ comment=\"$COMMENT\"\ experience=\"$EXPERIENCE\"\ gateway=\"$GATEWAY\"\ geometry=\"$GEOMETRY\"\ ip=\"$IP\"\ submenu=\"$SUBMENU\"\ title=\"$TITLE\"\ \/>"

#sed -e "s/^<rdp_server/$ADDLINE/1" $INFILE > $TMPFILE
sed -e "/^<rdp_server/a$ADDLINE/" $INFILE -e "/^<rdp_server/q" < $INFILE

Share this post


Link to post
Share on other sites

even more confused now.

In the same file I need to add at the end of each line to use it in latex, in this case I must put the sed command in single

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now