Retaining whitespaces...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Retaining whitespaces...
# 1  
Old 06-13-2013
Retaining whitespaces...

There's an input file(input.txt) which has the following details :
Code:
CBA
 BA

<Please note the second record has a LEADING WHITESPACE which is VALID>
I am using the following code to read the content of the said file line by line:
Code:
while read p ; do
   echo "$p"
done < input.txt

This is the output I am getting :
Code:
CBA
BA

Note : The leading WHITESPACE in the second record is getting removed automatically when the read command
is retrieving the values and assigning it to the variable "p"

HOW CAN I RETAIN THE LEADING WHITESPACE OF A COMMAND OUTPUT ????
IS IT A DEFAULT FEATURE OF UNIX TO REMOVE ALL LEADING/TRAILING WHITESPACE CHARACTERS WHENEVER A COMMAND ASSIGNS ITS OUTPUT TO A VARIABLE
FROM THE STDOUT ??
HOW TO BYPASS IT ??
This same thing happens for the following command also :

Code:
for p in `cat input.txt` ; do
   echo "$p"
done

PLEASE HELP.
Thanks
Kumarjit.

Last edited by radoulov; 06-13-2013 at 10:34 AM..
# 2  
Old 06-13-2013
It's a feature of the read builtin. It's designed to split on any characters in the IFS special variable, which default to whitespace, but can be set to whatever you want or even nothing.

Also, you should use read's -r switch, to disable backslash escapes, which read otherwise parses too.

Code:
while IFS="" read -r VARNAME
do
        echo "$VARNAME"
done

The for X in `cat file` loop is an even worse idea, because it doesn't split across lines -- it splits across any whitespace.

Last edited by Corona688; 06-13-2013 at 10:39 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Remove whitespaces between delimiter and characters

Hello All, I have a pipe delimited file and below is a sample data how it looks: CS123 | | || 5897 | QXCYN87876 As stated above, the delimited files contains sometimes only spaces as data fields and sometimes there are extra spaces before/after numeric/character data fields. My requirement... (4 Replies)
Discussion started by: amvip
4 Replies

2. Shell Programming and Scripting

Separate letters and replace whitespaces

Input file: aaaa bbb dd. qqq wwww e. Output file: a a a a <s> b b b <s> d d . q q q <s> w w w w <s> e . Can I use sed to do so in one step? (5 Replies)
Discussion started by: Viernes
5 Replies

3. Shell Programming and Scripting

Removing whitespaces from a line

HI I want to remove white spaces from a line I have a file like # cat /clsep 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 I need the output like 0 1 2 3 4 . . . 15 I feel sed command cannot be used.. I went through the man page of awk command and found (7 Replies)
Discussion started by: Priya Amaresh
7 Replies

4. Shell Programming and Scripting

Remove whitespaces in the n first characters?

I assume removing whitespaces in the n first characters of a string would be an easy task for sed? If so, how? (7 Replies)
Discussion started by: KidCactus
7 Replies

5. Shell Programming and Scripting

Substring in string with whitespaces

hello, i have this string: variable="1234 /PARAMETER_1:text /PARAMETER_2:othertext" i tried to do expr match $variable '.*\(*\)' but i keep getting expr error i need to extract the word text... thank you (4 Replies)
Discussion started by: Aloush89
4 Replies

6. Shell Programming and Scripting

if $variable has whitespaces?

Hi, Here's my requirement... There's a SFTP script with batchmode which logs in to the remote site and list all .done files to pick real files (see the following snippet) echo "-ls $FTP_PATH/*.done" > temp_file fileNames=`sftp -b temp_file $user@$ip` isDoneFile=`echo $fileNames |... (2 Replies)
Discussion started by: dips_ag
2 Replies

7. UNIX for Dummies Questions & Answers

Using grep to find multiple whitespaces

Hi All, Have a query on finding the lines with at least one whitespace. Have following lines in my file myfile.txt I I LOVE I LOVE FREEDOM I LOVE FREEDOM I LOVE FREEDOM AND PEACE I ran the following query to get the lines with at least 3 words. grep '\(* +\)\{3,\}' myfile.txt... (6 Replies)
Discussion started by: sh_kk
6 Replies

8. Shell Programming and Scripting

replace nulls with whitespaces in a file

Hi, i have a file which contains data in fixed length. each row contains data of 10 characters fixed length. The data in the file appears like 4567782882 some times i may recieve dat less than fixed length of 10. in such a case i find nulls appended at the trailing spaces when i do a... (2 Replies)
Discussion started by: meeragiri
2 Replies

9. UNIX for Dummies Questions & Answers

trimming whitespaces in a file

Hello, Im trying write a bash sript to search a file and identify any file names that contain a space at the end of them. I was trying to use the grep command but I can find how I would identify the character I'm looking for. Any help would be grately appreciated. (2 Replies)
Discussion started by: gintreach
2 Replies

10. Shell Programming and Scripting

help me to remove the whitespaces between the lines ...urgent

Hello all, i have a problem. please help me to remove the white spaces and tabs betweeen line. i.e., file1 contains some text.. text starts_hdsffdsd sdfsddssdds******** sdfsdsd*********** sdfsdsdfsdsdfsdsds*** ****fsd_test_ends one or 2 blank lines (* indicates white spaces or tabs) ... (5 Replies)
Discussion started by: kumar1
5 Replies
Login or Register to Ask a Question