Solved

remove spaces from  a input value

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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 …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

914 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

18 Experts available now in Live!

Get 1:1 Help Now