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 16 October 2008, 03:13 PM
  #1  
Markus
Scooby Regular
Thread Starter
 
Markus's Avatar
 
Join Date: Mar 1999
Location: The Great White North
Posts: 25,080
Likes: 0
Received 0 Likes on 0 Posts
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?
Old 17 October 2008, 02:52 PM
  #2  
P1_BEN
Scooby Regular
 
P1_BEN's Avatar
 
Join Date: Apr 2002
Location: Kent
Posts: 460
Likes: 0
Received 0 Likes on 0 Posts
Default

Hi Markus

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

Thanks, much appreciated and exactly what I needed.
Old 17 October 2008, 09:09 PM
  #4  
druddle
Scooby Regular
 
druddle's Avatar
 
Join Date: Mar 2001
Location: Berkshire
Posts: 5,528
Likes: 0
Received 0 Likes on 0 Posts
Default

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



Quick Reply: Shell script help



All times are GMT +1. The time now is 05:31 AM.