Periods turn into spaces for some reason


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Periods turn into spaces for some reason
# 1  
Old 01-17-2013
Periods turn into spaces for some reason

Hey all,

I've come across a problem that I can't solve. Its as simple as it can get.
I want to substitute spaces in a string with dots e.g.
Code:
i am a string 
i.am.a.string

I've tried examples from the web that should work but they don't.
A period seems to become a space for some reason :s

One of the examples I used is this one:
Code:
echo ${zone// /"."}

zone being a string and replacing the "." with a "_" results in the spaces being substituted with underscores (which is the result I need) but it only works with underscores and not with periods as in the example. Instead of dots I get spaces.

Anyone know why this is and how to fix it?

Last edited by Scrutinizer; 06-27-2013 at 01:29 PM.. Reason: code tags
# 2  
Old 01-17-2013
Code:
echo 'i   am a string' | sed 's/ /./g'

# 3  
Old 01-17-2013
This works for me:
Code:
echo ${zone// /.}

# 4  
Old 01-17-2013
found the problem...
in the beginning of my script I issued IFS to split on periods and I don't know why (I know you guys do Smilie ) but I had to set IFS to " " i.e. a space in order to make it work
This User Gave Thanks to basherlemon For This Post:
# 5  
Old 01-17-2013
This is why you should always post your script, not just the tiny bit of it you think's the problem. Sometimes it's taken pages of yelling to discover they changed IFS somewhere Smilie

Whenever I must change IFS, this is what I do:

Code:
# Sometimes you can get away with just prefixing it
while IFS="." read A B C

If I must change it for a whole block I do this:

Code:
OLDIFS="$IFS"
IFS="."
        # Indented section where IFS is weird
# Put back the default
IFS="$OLDIFS"


Last edited by Corona688; 01-17-2013 at 04:23 PM..
# 6  
Old 01-17-2013
You can also use `tr`
Code:
echo $zone | tr -s " " "."

Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Split and name files as 15 minute periods

Hi, I have a number of large files (up to 4GB) that I wish to split in 96 parts e.g. one for each 15 minutes of the day. The split can be random so I am using split -b 42M filename.csv I want to name each of the resultant files as a distinct 15 minute period of the day e.g. ... (10 Replies)
Discussion started by: ksexton
10 Replies

2. UNIX for Advanced & Expert Users

get the file extension having multiple periods

How to get the file extension having multiple periods suppose i have a file samplewprk.txt.dat I need to retrieve txt.dat from the file. I worked all iam getting is either txt or pyd Could anyone help me in providing the solution? Thanks, in advance (3 Replies)
Discussion started by: chinku
3 Replies

3. UNIX for Dummies Questions & Answers

backslashing periods

Hello, I have a script which configures a system, the configuration is currently manual and error prone. I am writting a script, which currently uses hard-coded values. I don't know how to take an IP, e.g. 123.456.789.111, and backslash the periods, so I can pass it to an `exec perl... (0 Replies)
Discussion started by: Bloke
0 Replies

4. Shell Programming and Scripting

Removing blank spaces, tab spaces from file

Hello All, I am trying to remove all tabspaces and all blankspaces from my file using sed & awk, but not getting proper code. Please help me out. My file is like this (<b> means one blank space, <t> means one tab space)- $ cat file NARESH<b><b><b>KUMAR<t><t>PRADHAN... (3 Replies)
Discussion started by: NARESH1302
3 Replies

5. UNIX for Dummies Questions & Answers

how to append spaces(say 10 spaces) at the end of each line based on the length of th

Hi, I have a problem where I need to append few spaces(say 10 spaces) for each line in a file whose length is say(100 chars) and others leave as it is. I tried to find the length of each line and then if the length is say 100 chars then tried to write those lines into another file and use a sed... (17 Replies)
Discussion started by: prathima
17 Replies

6. Shell Programming and Scripting

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies

7. Programming

what is the exact reason ?

please refer the following 2 statements... 1) int i=1,j; j= i++ + i++; 2) int i=1,j; j=++i + ++i; how j becomes 2 and 6 for the above 2 statements respectively ??? ( i guessed j must be 3 and 5) Somebody define the exact reason please.. :( ... (2 Replies)
Discussion started by: shamal
2 Replies
Login or Register to Ask a Question