Solved

remove spaces from  a input value

Posted on 2014-09-10
6
228 Views
Last Modified: 2014-09-16
Hello,
I am trying to remove spaces from a input value to my script.  Need some help with the syntax,

for example,

number=$1
customer=$2
loaction=$3

newcustomer=$(awk '{gsub(" ","-",$2);print $2}')
echo $newcustomer

Thanks,
0
Comment
Question by:bje
6 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 40314977
Do you really mean "translate spaces in awk's $2 (= field #2 of the input string, which may be the content of $customer ) to hyphens and store the result in newcustomer"? To make this work you'll need a field separator other than "space" in $customer, and you must put "$2" at the shell level between quotes.

customer="$2"

For bash:

newcustomer=$(awk -F"field_separator" '{gsub(" ","-",$2);print $2}' <<< $customer)

For ksh (works in bash as well):

newcustomer=$(echo $customer | awk -F"field_separator" '{gsub(" ","-",$2);print $2}')

But if you mean "translate spaces in $customer to hyphens and store the result in newcustomer":

customer="$2"

newcustomer=$(awk '{gsub(" ","-");print}' <<< $customer)

newcustomer=$(echo $customer | awk '{gsub(" ","-");print}')

Please remember that you must call your script like this (mind the quotes):

scriptname number "cus to mer" location

If "number" or "location" can contain spaces you must enclose the corresponding input values in quotes as well:

scriptname "num ber" "cus to mer" "locat ion"
0
 
LVL 19

Expert Comment

by:simon3270
ID: 40315813
If you are using bash you can remove spaces with:

    newcustomer=${customer// /}

or change spaces to hyphens with

    newcustomer=${customer// /-}

(Edit: changed to "newcustomer" to match existing examples)
0
 
LVL 62

Expert Comment

by:gheist
ID: 40316538
sed s/ //g
0
 
LVL 19

Expert Comment

by:simon3270
ID: 40316997
If you want (or need) to use an external program, you are better off using the one designed to replace characters - tr

To replace spaces with hyphens:
    newcustomer=$(echo $customer | tr ' ' '-')

Open in new window

or to delete spaces:
    newcustomer=$(echo $customer | tr -d ' ')

Open in new window

0
 

Author Comment

by:bje
ID: 40320178
thank you for all the solutions
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Utilizing an array to gracefully append to a list of EmailAddresses
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question