ScoobyNet.com - Subaru Enthusiast Forum

ScoobyNet.com - Subaru Enthusiast Forum (https://www.scoobynet.com/)
-   Computer & Technology Related (https://www.scoobynet.com/computer-and-technology-related-34/)
-   -   Shell script help (https://www.scoobynet.com/computer-and-technology-related-34/718958-shell-script-help.html)

Markus 16 October 2008 03:13 PM

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?

P1_BEN 17 October 2008 02:52 PM

Hi Markus

echo “(space)(space)This(space)is(space)a(space)test(sp ace)(space)(space) to” | sed -e"s/ */ /g"

Markus 17 October 2008 07:54 PM

Thanks, much appreciated and exactly what I needed.

druddle 17 October 2008 09:09 PM

In UNIX land, sed is your best friend :D :D


All times are GMT +1. The time now is 04:18 AM.


© 2024 MH Sub I, LLC dba Internet Brands