Solved

remove spaces from  a input value

Posted on 2014-09-10
6
245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

729 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