Consecutive spaces within input being converted to single space


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Consecutive spaces within input being converted to single space
# 1  
Old 08-31-2007
Consecutive spaces within input being converted to single space

I'm reading from a file that is semi-colon delimited. One of the fields contains 2 spaces separating the first and last name (4th field in - "JOHN<space><space> DOE"):

e.g. TORONTO;ONTARIO;1 YONGE STREET;JOHN DOE;CANADA

When I read this record and either echo/print to screen or write to output, the 2 spaces get converted to a single space. I need to keep all spaces intact when writing to output.

Here's a snippet of code I'm using. I'm attempting to load a series of records into an array with the purpose of performing logic on the contents and then outputting to several different output(s):

exec 3<$1 # Open input
exec 4>$1.out # Open output
let CNF_REC_CNT=CNF_REC_CNT+1
read -u3 REC_BUF[CNF_REC_CNT]
print "Buffer is : "${REC_BUF[CNF_REC_CNT]}""

How do I get around this?
# 2  
Old 08-31-2007
try:
Code:
IFS="" && read  REC_BUF[CNF_REC_CNT]
IFS=" "

# 3  
Old 08-31-2007
Still doesn't work. Thx for trying. Getting same results.
# 4  
Old 08-31-2007
You need to put the variable inside single quotes when you print it. You think you did this but you didn't:

print "Buffer is : "${REC_BUF[CNF_REC_CNT]}""

First quoted string:"Buffer is : "
Unquoted string: ${REC_BUF[CNF_REC_CNT]}
Second quoted string: ""

Maybe you wanted:
print "Buffer is : \"${REC_BUF[CNF_REC_CNT]}\""
which is now a single quoted string.
# 5  
Old 09-04-2007
That may have been a typo on my part. My program does write to output ok except for the fact that the double space comes out as a single space.

I'm going to ditch using the 'read' command and try using a combination of cat, grep and awk to rip through the file. If I do a :

REC_BUF=`grep '^' $1`

and then output to screen, the 2 spaces appear. Therefore I know it's a 'read' specific feature.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing Multiple spaces with a single space but excluding few regular expressions

Hi All. Attached are two files. I ran a query and have the output as in the file with name "FILEWITHFOURRECORDS.txt " I didn't want all the spaces between the columns so I squeezed the spaces with the "tr" command and also added a carriage return at the end of every line. But in two... (3 Replies)
Discussion started by: sparks
3 Replies

2. Shell Programming and Scripting

Script to code every 2 consecutive entries as single entry

All, I come across the below requirement and my search on the previous posts did not result into any matches. I have one column of data from a csv file like below. And I need to add additional column based on string count in first column. Given column, Required column, Other columns A, 1,... (8 Replies)
Discussion started by: ks_reddy
8 Replies

3. UNIX for Dummies Questions & Answers

Replacing double spaces with single space

I am looking for a regular expression that uses sed to replace multiple spaces with single spaces on every line where it is not at the start of the line and not immediately before double slashes ('//') or between quotes ("). In its simplest form, it would look like this: sed -e 's# # #g'... (4 Replies)
Discussion started by: figaro
4 Replies

4. Shell Programming and Scripting

Shell script - entered input(1-40 bytes) needs to be converted exactly 40 bytes

hello, suppose, entered input is of 1-40 bytes, i need it to be converted to 40 bytes exactly. example: if i have entered my name anywhere between 1-40 i want it to be stored with 40 bytes exactly. enter your name: donald duck (this is of 11 bytes) expected is as below - display 11... (3 Replies)
Discussion started by: shravan.300
3 Replies

5. Shell Programming and Scripting

how to keep tab from being converted to space

Hi, I want to read lines from a file, and I'm using two methods 1 use while read line do done<filename 2 use line=`sed -n '3p' filename` however, in both of them, I notice that the tab between fields are automatically converted to space because I want to use awk over the... (10 Replies)
Discussion started by: esolvepolito
10 Replies

6. Shell Programming and Scripting

function terminating if i give input as space or no input and enter

HI i have written a script to ask input from the user. this script should promote the user for y/n input. if user enters anyother input then y/n the script promotes him again. this below code is working fine for all the cases. except for space and enter " if i give space and enter it is... (2 Replies)
Discussion started by: BHASKARREDDY006
2 Replies

7. UNIX for Dummies Questions & Answers

How to translate multiple spaces into a single space using tr command?

I am trying to read a txt file and trying to translate multiples spaces into single spaces so the file is more organized, but whenever I try the command: tr ' ' ' ' w.txt The output is: tr: extra operand `w.txt' Try `tr --help' for more information. Can someone please help? :wall: ... (2 Replies)
Discussion started by: Nonito84
2 Replies

8. Shell Programming and Scripting

How to avoid the truncating of multiple spaces into a single space while reading a line from a file?

consider the small piece of code while read line do echo $line done < example content of example file sadasdasdasdsa erwerewrwr ergdgdfgf rgerg erwererwr the output is like sadasdasdasdsa erwerewrwr ergdgdfgf rgerg erwererwr the... (4 Replies)
Discussion started by: Kesavan
4 Replies

9. Shell Programming and Scripting

replace space or spaces in a line of a file with a single :

I am searching while I await a response to this so if it has been asked already I apologize. I have a file with lines in it that look like: bob johnson email@email.org I need it to look like: bob:johnson:email@email.org I am trying to use sed like this: sed -e 's/ /:/g' file >... (5 Replies)
Discussion started by: NewSolarisAdmin
5 Replies

10. UNIX for Dummies Questions & Answers

how to read double consecutive space in filename for bash shell

Hello, Im using cygwin app which we use the bash shell for scripting. Im trying to read a filename with consecutive spaces on the filename ex: filename<space><space>1.txt ls -lrt filename<space><space>1.txt is working but when I pull that from a file and place it into variable. it reads... (1 Reply)
Discussion started by: james_falco
1 Replies
Login or Register to Ask a Question