Solved

remove spaces from  a input value

Posted on 2014-09-10
6
220 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 61

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
Utilizing an array to gracefully append to a list of EmailAddresses
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now