prob with spaces in reading a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting prob with spaces in reading a file
# 1  
Old 05-23-2006
prob with spaces in reading a file

i have a file in unix having data like
cat
dog
(having spaces in the beginning)
when i read it in a shell script
cat file_name |while read line
do
echo $line
done
it will print
cat
dog
spaces at the beginning are removed....

i dont want these spaces to be removed...it shud print like
cat
dog

is there anyway out ?
Thanks in advance
# 2  
Old 05-23-2006
By default, the internal field separator IFS is set to space, linefeed, and tab. By changing that in your script, you can maintain the spaces. Try this

Code:
#!/bin/ksh

export IFS=$(echo "\n\t")

cat file_name | while read line
do
    echo $line
done

I'm sure there are better ways to reset IFS but I've been doing it this way for so long...I have forgotten why.
# 3  
Old 05-23-2006
thnx a lot Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with reading directory paths with spaces from a file

Hi I want to know how to handle the spaces in the below scenario. I have a file (CON_zip_path_1.txt) which has some directory paths with spaces in directory names . My requirement is to unzip these zip files to another path. Please see the code below and the error. CON_zip_path_1.txt... (4 Replies)
Discussion started by: paul1234
4 Replies

2. UNIX for Dummies Questions & Answers

Reading filenames with spaces

Hello I've got a certain no. of files in a directory whose names I'm reading and redirecting into a temporary text file using the command below: ls -l | grep ^- | awk '{print $9}'However, whenever the file names contain spaces the above command considers only the part of the file name up to... (5 Replies)
Discussion started by: S. BASU
5 Replies

3. Shell Programming and Scripting

Reading in data that has spaces in it.

I have a csv file called template.csv which has the following data Name, Age, Height Jessica Jesse, 18, 150 Now what I want to do is use a shell script to read the name age and height which looks like this: #!bin/sh INPUT='template.csv while read Name Age Height do echo... (2 Replies)
Discussion started by: JSNY
2 Replies

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

5. Shell Programming and Scripting

help with echo command in reading blank spaces in aix

Hi, I have a test.txt and would like to put in a different file but while putting in the other file, blank spaces are missing. $ indicates blank spaces filename: test.txt: USA$$$840$$$$ Desired output USA$$$840$$$$ Current output as per the following code. while read j; do... (3 Replies)
Discussion started by: techmoris
3 Replies

6. Shell Programming and Scripting

Reading complete line after spaces

Hi, I am reading data from a variable which has spaces in it. I want to get the data after first space, i.e. if my data line is "My Name is Ashish...", I want the data returned as "Name is Ashish". I am using #!/bin/sh shell. Please help me with the code to read the complete data after first... (8 Replies)
Discussion started by: gupt_ash
8 Replies

7. Shell Programming and Scripting

reading in arguments with spaces

I want to be able to read in input which contain spaces and put that into an array. Each field should be delimeted by a space and should be a different array element. However I cant get it to work. any tips? read input //type field1 field2 field3 echo "$input" array="$input" Thanks in... (11 Replies)
Discussion started by: Calypso
11 Replies

8. Shell Programming and Scripting

Reading a file using sh with spaces in filename

Hi I am trouble parsing through a file with spaces in the filename. I need to grab "supportIDPS/SCM/windows_install/file groups/dds.fgl" and then do a md5sum on it. I am using sh. Any help is appreciated. Here is an example of the input file: 7eedbc9f7902bf4c1878d9e571addf9a ... (4 Replies)
Discussion started by: jekl5
4 Replies

9. UNIX for Dummies Questions & Answers

Reading a line including spaces

Hi All, I have a script that reads a file and echo it back to std out. Test.txt 1aaaaaaaaaaa . The script is ReadLine.sh #!/bin/ksh cat $1 | while read file do echo $file done I invoke the script as ReadLine.sh Test.txt The output that I get is (1 Reply)
Discussion started by: aksarben
1 Replies

10. UNIX for Advanced & Expert Users

Please Help!! Reading a string of text with concurrent spaces into a shell variable

Please Help!! Here is a very simplistic example of what I am trying to accomplish. I need what I have inbetween the quotes to be read into the shell variable. x="This is fun" echo $x The results of x from the above expression is: This is fun Notice the unix takes out the... (1 Reply)
Discussion started by: mjs3221
1 Replies
Login or Register to Ask a Question