NEWBIE: SED in Shell Script

NEWBIE: SED in Shell Script

Post by Martin McWhorte » Thu, 04 May 2000 04:00:00



Hello,

I am editing a shell script that containes a vairable $MACHINE. Machine
equals something like beck.router

I need to sed s/\\./,/g the $MACHINE variable to change it to
beck,router

How do I do this? I seem lost. I know in perl is would be $MACHINE =~
s/\\./,/g but how does this translate to shell script?

Thanks,
Martin

 
 
 

NEWBIE: SED in Shell Script

Post by Matthew Land » Thu, 04 May 2000 04:00:00



> Hello,

> I am editing a shell script that containes a vairable $MACHINE. Machine
> equals something like

> I need to sed s/\\./,/g the $MACHINE variable to change it to
> beck,router

> How do I do this? I seem lost. I know in perl is would be $MACHINE =~
> s/\\./,/g but how does this translate to shell script?

> Thanks,
> Martin

How about (ksh)

MACHINE=$(echo $MACHINE |tr '.' ',')

or

MACHINE=$(echo $MACHINE | sed 's/\./,/g')

 - Matt

--
_______________________________________________________________________

   << Comments, views, and opinions are mine alone, not IBM's. >>

 
 
 

NEWBIE: SED in Shell Script

Post by Charles Dem » Thu, 04 May 2000 04:00:00




Quote:>Hello,

>I am editing a shell script that containes a vairable $MACHINE. Machine
>equals something like beck.router

>I need to sed s/\\./,/g the $MACHINE variable to change it to
>beck,router

>How do I do this? I seem lost. I know in perl is would be $MACHINE =~
>s/\\./,/g but how does this translate to shell script?

>Thanks,
>Martin

In a bourne type shell this should work:

MACHINE=`echo "$MACHINE" | sed 's/\\./,/g'`

This is usually explained on the man page for your shell

man sh

or whatever shell you're using.  Note that csh and tcsh are
different than bourne type shells, and are not generally
recommended for scripts.

Chuck Demas
Needham, Mass.

--
  Eat Healthy    |   _ _   | Nothing would be done at all,

  Die Anyway     |    v    | That no one could find fault with it.

 
 
 

NEWBIE: SED in Shell Script

Post by Christoph Lang » Thu, 04 May 2000 04:00:00



> Hello,
> I am editing a shell script that containes a vairable $MACHINE. Machine
> equals something like beck.router
> I need to sed s/\\./,/g the $MACHINE variable to change it to
> beck,router
> How do I do this? I seem lost. I know in perl is would be $MACHINE =~
> s/\\./,/g but how does this translate to shell script?

MACHINE=`echo $MACHINE | sed 's/\./,/g'`

Within '...', there's no need to quote the backslash as \\.

Christoph

--
The church is near but the road is icy,
the bar is far away but I will walk carefully.
-- Russian Proverb
Christoph Lange

 
 
 

NEWBIE: SED in Shell Script

Post by bmar.. » Fri, 05 May 2000 04:00:00



   >Hello,
   >I am editing a shell script that containes a vairable $MACHINE.
   >Machine equals something like beck.router
   >I need to sed s/\\./,/g the $MACHINE variable to change it to
   >beck,router
   >How do I do this? I seem lost. I know in perl is would be $MACHINE
   >=~ s/\\./,/g but how does this translate to shell script?
bash, ksh, posix sh:
MACHINE=$(echo $MACHINE | sed 's/\./,/g')
most other shells:
MACHINE=`echo $MACHINE | sed 's/\./,/g'`

Net-Tamer V 1.08X - Test Drive

 
 
 

1. sed problem: using sed in a shell script

I am having a very odd problem. The following command is not working
in my shell script:

#!/bin/sh
# snip code #
sed "$start_line,$end_line""d" $1.tmp > $1.new
# END snip #

But, it _does_ work if i enter it at the command-line:
[eve]$  set start_line=2 ; set end_line=4 ; \
sed "$start_line,$end_line""d" tokenize_line.c.tmp > tokenize_line.c.new

This works on the command-line, but not in the script as well:
[eve]$ set start_line=2 ; set end_line=4 ; \
sed "$start_line,${end_line}d" tokenize_line.c.tmp > tokenize_line.c.new

then odd part is that if i set the variables in the script manually,
everything works ok:
#!/bin/sh
# snip #
startline=2; endline=4
sed "$start_line,$end_line""d" $1.tmp > $1.new
# END snip #

so i figured.. there must be space or something before/after the
numbers in the variables. but that is disproved, because i tried this
in the original shell script right before calling sed:
#snip
echo "cutting lines foo${start_line}foo to foo${end_line}foo"
#end snip

and that outputs this (no spaces!):
$
cutting lines foo2foo to foo4foo

so basically, i need to cut line ranges from files, and sed is the
only way i know how to do it, and it dont work (when interpolating
variables) in the shell script. any ideas here? (oh.. my shell is
tcsh)

--
MGatto
Support the anti-spam movement; see <http://www.cauce.org/>

2. What's up with the ports?!?

3. . sed script unix shell script ispell

4. GNOME TASK BAR

5. . sed script offered, sed script wanted

6. Starting point for new user?

7. Pointers/Help with a basic shell script (Shell Newbie alert)

8. How do I get rid of mouse?

9. Newbie help with Shell scripts (using Korn shell)

10. NEWBIE-Shell scripting - When to use script variable vs. create tmp file???

11. sed works in shell, but not in a script

12. Bourne shell CGI script and sed

13. shell script and sed