Jump to content


Photo

Sed Help


  • Please log in to reply
13 replies to this topic

#1 iccaros

iccaros

    UberTechie

  • Linux Experts
  • 1292 posts
  • Location:Great State of Washingtion
  • Operating System:Gentoo,Iccaros-Linux(of course),Slackware,GentooX,Red Hat, Windows (3.1 to VISTA BETA), MAC OSX (10.4 currently),LFS, Solaris 8,9,10, Trusted Solaris, FreeBSD, OPENBSD, NETBSD

Posted 08 April 2009 - 09:31 PM

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

#2 jcl

jcl

    UberTechie

  • Linux Experts
  • 1304 posts
  • Location:The Internet
  • Operating System:Arch

Posted 08 April 2009 - 11:18 PM

Drop the eval.

#3 iccaros

iccaros

    UberTechie

  • Linux Experts
  • 1292 posts
  • Location:Great State of Washingtion
  • Operating System:Gentoo,Iccaros-Linux(of course),Slackware,GentooX,Red Hat, Windows (3.1 to VISTA BETA), MAC OSX (10.4 currently),LFS, Solaris 8,9,10, Trusted Solaris, FreeBSD, OPENBSD, NETBSD

Posted 08 April 2009 - 11:30 PM

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


#4 iccaros

iccaros

    UberTechie

  • Linux Experts
  • 1292 posts
  • Location:Great State of Washingtion
  • Operating System:Gentoo,Iccaros-Linux(of course),Slackware,GentooX,Red Hat, Windows (3.1 to VISTA BETA), MAC OSX (10.4 currently),LFS, Solaris 8,9,10, Trusted Solaris, FreeBSD, OPENBSD, NETBSD

Posted 08 April 2009 - 11:35 PM

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

#5 iccaros

iccaros

    UberTechie

  • Linux Experts
  • 1292 posts
  • Location:Great State of Washingtion
  • Operating System:Gentoo,Iccaros-Linux(of course),Slackware,GentooX,Red Hat, Windows (3.1 to VISTA BETA), MAC OSX (10.4 currently),LFS, Solaris 8,9,10, Trusted Solaris, FreeBSD, OPENBSD, NETBSD

Posted 08 April 2009 - 11:42 PM

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)

#6 jcl

jcl

    UberTechie

  • Linux Experts
  • 1304 posts
  • Location:The Internet
  • Operating System:Arch

Posted 08 April 2009 - 11:44 PM

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


#7 iccaros

iccaros

    UberTechie

  • Linux Experts
  • 1292 posts
  • Location:Great State of Washingtion
  • Operating System:Gentoo,Iccaros-Linux(of course),Slackware,GentooX,Red Hat, Windows (3.1 to VISTA BETA), MAC OSX (10.4 currently),LFS, Solaris 8,9,10, Trusted Solaris, FreeBSD, OPENBSD, NETBSD

Posted 09 April 2009 - 12:08 AM

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

#8 jcl

jcl

    UberTechie

  • Linux Experts
  • 1304 posts
  • Location:The Internet
  • Operating System:Arch

Posted 09 April 2009 - 12:14 AM

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?

#9 iccaros

iccaros

    UberTechie

  • Linux Experts
  • 1292 posts
  • Location:Great State of Washingtion
  • Operating System:Gentoo,Iccaros-Linux(of course),Slackware,GentooX,Red Hat, Windows (3.1 to VISTA BETA), MAC OSX (10.4 currently),LFS, Solaris 8,9,10, Trusted Solaris, FreeBSD, OPENBSD, NETBSD

Posted 09 April 2009 - 12:44 AM

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

#10 iccaros

iccaros

    UberTechie

  • Linux Experts
  • 1292 posts
  • Location:Great State of Washingtion
  • Operating System:Gentoo,Iccaros-Linux(of course),Slackware,GentooX,Red Hat, Windows (3.1 to VISTA BETA), MAC OSX (10.4 currently),LFS, Solaris 8,9,10, Trusted Solaris, FreeBSD, OPENBSD, NETBSD

Posted 09 April 2009 - 12:49 AM


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..

#11 jcl

jcl

    UberTechie

  • Linux Experts
  • 1304 posts
  • Location:The Internet
  • Operating System:Arch

Posted 09 April 2009 - 12:53 AM

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

#12 iccaros

iccaros

    UberTechie

  • Linux Experts
  • 1292 posts
  • Location:Great State of Washingtion
  • Operating System:Gentoo,Iccaros-Linux(of course),Slackware,GentooX,Red Hat, Windows (3.1 to VISTA BETA), MAC OSX (10.4 currently),LFS, Solaris 8,9,10, Trusted Solaris, FreeBSD, OPENBSD, NETBSD

Posted 09 April 2009 - 01:13 AM

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..

#13 iccaros

iccaros

    UberTechie

  • Linux Experts
  • 1292 posts
  • Location:Great State of Washingtion
  • Operating System:Gentoo,Iccaros-Linux(of course),Slackware,GentooX,Red Hat, Windows (3.1 to VISTA BETA), MAC OSX (10.4 currently),LFS, Solaris 8,9,10, Trusted Solaris, FreeBSD, OPENBSD, NETBSD

Posted 09 April 2009 - 01:41 AM

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


#14 ibionika

ibionika

    Member

  • Members
  • Pip
  • 6 posts
  • Location:Russian IP
  • Operating System:Russian spam system

Posted 26 June 2009 - 10:42 AM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users