Solved

remove spaces from  a input value

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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
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 …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

738 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