Sponsored Content
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? Post 302510761 by Chubler_XL on Tuesday 5th of April 2011 12:03:54 AM
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

 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
truncate(2)							System Calls Manual						       truncate(2)

Name
       truncate, ftruncate - truncate a file to a specified length

Syntax
       truncate(path, length)
       char *path;
       int length;

       ftruncate(fd, length)
       int fd, length;

Description
       The  system  call  causes the file named by path or referenced by fd to be truncated to, at most, length bytes in size.	If the file previ-
       ously was larger than this size, the extra data is lost.  With the file must be open for writing.

Return Values
       A value of zero (0) is returned if the call succeeds.  If the call fails, a -1 is returned, and the global  variable  errno  specifies  the
       error.

Restrictions
       Partial blocks discarded as the result of truncation are not zero-filled. This can result in holes in files that do not read as zero.

Diagnostics
       The system call succeeds unless:

       [ENOTDIR]      A component of the path prefix is not a directory.

       [ENOENT]       The named file does not exist.

       [EACCES]       Search permission is denied for a component of the path prefix.

       [EISDIR]       The named file is a directory.

       [EROFS]	      The named file resides on a read-only file system.

       [ETXTBSY]      The file is a pure procedure (shared text) file that is being executed.

       [EFAULT]       The path points outside the process's allocated address space.

       [ENAMETOOLONG] A component of a pathname exceeded 255 characters, or an entire path name exceeded 1023 characters.

       [ELOOP]	      Too many symbolic links were encountered in translating the pathname.

       [EIO]	      An I/O error occurred updating the inode.

       The system call succeeds unless:

       [EBADF]	      The fd is not a valid descriptor.

       [EINVAL]       The fd references a socket, not a file.

       [ETIMEDOUT]    A  connect  request  or remote file operation failed, because the connected party did not properly respond after a period of
		      time that is dependent on the communications protocol.

See Also
       open(2)

																       truncate(2)
All times are GMT -4. The time now is 03:12 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy