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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to avoid the truncating of multiple spaces into a single space while reading a line from a file?
# 1  
Old 10-21-2010
MySQL 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
Code:
while read line
do
echo $line
done < example

content of example file
Code:
sadasdasdasdsa                          erwerewrwr  ergdgdfgf rgerg             erwererwr

the output is like
Code:
sadasdasdasdsa erwerewrwr ergdgdfgf rgerg erwererwr

the multiple spaces are getting truncated while reading, is there any way to avoid that??Smilie
# 2  
Old 10-21-2010
Quote the variable:
Code:
echo "$line"

# 3  
Old 10-21-2010
Quote:
Originally Posted by Franklin52
Quote the variable:
Code:
echo "$line"

Thankyou Franklin.....Smilie
# 4  
Old 04-05-2011
This will still truncate any leading and trailing spaces.

If you need to keep them you can just use read and REPLY:

Code:
while read
do
    echo "$REPLY"
done < example

# 5  
Old 04-05-2011
MySQL

Thank you ChublerSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

How to avoid truncating in ps output ?

Hello, This is Solaris 10 (x86) bash-3.2# cat /etc/release Solaris 10 5/09 s10x_u7wos_08 X86 Copyright 2009 Sun Microsystems, Inc. All Rights Reserved. Use is subject to license terms. Assembled 30 March... (5 Replies)
Discussion started by: solaris_1977
5 Replies

2. 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

3. Shell Programming and Scripting

awk - CSV file - field with single or multiple spaces

Hi, In a csv file, I want to select records where first column has zero or multiple spaces. Eg: abc.csv ,123,a ,22,b ,11,c a,11,d So output should be: ,123,a ,22,b ,11,c Please advise (5 Replies)
Discussion started by: vegasluxor
5 Replies

4. Shell Programming and Scripting

Avoid single line output from function

I am new to shell scripting and wished to get few things clarified. While calling functions within shell script, output comes out as single line irrespective of the no of echos or newlines I tried within function + the echo -e used to invoke function ( as instructed online) : #!/bin/sh inc() {... (1 Reply)
Discussion started by: RMath
1 Replies

5. Shell Programming and Scripting

How to ignore single or multiple lines between /* and */ while reading from a file in unix?

I have a file proc.txt: if @debug = 1 then message 'Start Processing ', @procname, dateformat(now(*), 'hh:mm:ss'), @julian type info to client; end if; /* execute immediate with quotes 'insert into sys_suppdata (property, value, key_name) location ''' || @supp_server || '.' ||... (5 Replies)
Discussion started by: kidncute
5 Replies

6. 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

7. Shell Programming and Scripting

Reading a single character from each line of the file

Hi, I should read one character at a fixed position from each line of the file. So how ??? should be substituted in the code below: while read line ; do single_char=`???` echo "$single_char" done < $input_file (8 Replies)
Discussion started by: arsii
8 Replies

8. 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

9. UNIX for Dummies Questions & Answers

echo is suppressing multiple spaces.How to avoid it

I am reading a file and copying selected lines from the file into another using echo. For eg: while read line do if ((some logic to determine whether I need to copy the current line to another file)) then echo $line > tempfile fi done<srcfile The problem I have is the data in the file... (1 Reply)
Discussion started by: prathima
1 Replies

10. Shell Programming and Scripting

Reading Multiple Variables From a Single Line in Shell

I'm a Linux newb, I've been running a Debian Linux server for about a year now, and I've written some simple scripts to automate various things, but I still don't know much, and I forget what I learn as fast as I figure it out... Anyway, that really isn't important, I just want you to know that... (14 Replies)
Discussion started by: Drek
14 Replies
Login or Register to Ask a Question