Notices
Computer & Technology Related Post here for help and discussion of computing and related technology. Internet, TVs, phones, consoles, computers, tablets and any other gadgets.

Shell script help

Thread Tools
 
Search this Thread
 
Old Oct 16, 2008 | 03:13 PM
  #1  
Markus's Avatar
Markus
Thread Starter
Scooby Regular
25 Year Member
 
Joined: Mar 1999
Posts: 25,080
Likes: 0
From: The Great White North
Default Shell script help

Morning/Afternoon All,
Once again I need to pick the brains of the unix guru's.

I'm writing a shell script (OS is Mac OS X 10.4 / 10.5, typically using sh as the interpreter, but I guess I could use bash instead) and what I want to do is to remove leading and trailing spaces from a variable in the script (it's actually argument 1 that's passed to the script) but to retain any spaces that are within the actual variable.

Here's an example of what the variable could contain:

(space)(space)This(space)is(space)a(space)test(spa ce)(space)(space)

The result after processing I would like is:

This(space)is(space)a(space)test

So, chop off leading and trailing, but keep the spaces within the actual string.

How could I accomplish this?
Reply
Old Oct 17, 2008 | 02:52 PM
  #2  
P1_BEN's Avatar
P1_BEN
Scooby Regular
 
Joined: Apr 2002
Posts: 460
Likes: 0
From: Kent
Default

Hi Markus

echo “(space)(space)This(space)is(space)a(space)test(sp ace)(space)(space) to” | sed -e"s/ */ /g"
Reply
Old Oct 17, 2008 | 07:54 PM
  #3  
Markus's Avatar
Markus
Thread Starter
Scooby Regular
25 Year Member
 
Joined: Mar 1999
Posts: 25,080
Likes: 0
From: The Great White North
Default

Thanks, much appreciated and exactly what I needed.
Reply
Old Oct 17, 2008 | 09:09 PM
  #4  
druddle's Avatar
druddle
Scooby Regular
 
Joined: Mar 2001
Posts: 5,528
Likes: 0
From: Berkshire
Default

In UNIX land, sed is your best friend
Reply
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
Abx
Subaru
22
Jan 9, 2016 05:42 PM
Mattybr5@MB Developments
Full Cars Breaking For Spares
28
Dec 28, 2015 11:07 PM
Mattybr5@MB Developments
Full Cars Breaking For Spares
12
Nov 18, 2015 07:03 AM
scoob1985
Wanted
0
Sep 13, 2015 12:06 PM




All times are GMT +1. The time now is 01:37 PM.