Read files including spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read files including spaces
# 1  
Old 06-06-2005
Read files including spaces

I am accessing two files. I am using read command to read from the files.

For the first file, I need read the fields delimited by spaces, and for the other file, I need to read the whole line as a single field including the spaces.

When I used read command for the second file, the spaces are removed when read command is used.

I cannot use IFS="" because for the other file, I want the spaces as the delimiters.

Can anyone help??

Thanks,
kumariak
# 2  
Old 06-06-2005
Quote:
Originally Posted by kumariak
I am accessing two files. I am using read command to read from the files.
fil1 contains the following

Quote:
A b c d e
e d c b A
fil2 contains the following

Quote:
Abcde
edcbA
The following code test.sh

Code:
#! /usr/bin/ksh
echo "processing file 1 delimited by spaces"
IFS=" "
while read line
do
echo $line
done < fil1

echo "processing file 2 delimited by EOL"
IFS=\$
while read line
do
echo $line
done < fil2

will output the following

processing file 1 delimited by spaces
A b c d e
e d c b A
processing file 2 delimited by EOL
Abcde
edcbA


Is this what you were looking for?
# 3  
Old 06-06-2005
I suppose the problem is in fact a confusion of what "echo" does with your variables.

"read" is always providing you with the full content of the line, but "echo" (as well as "print", etc.) will interpret your variables, which may change their content.

To prevent this, just enclose your variables in double quotes. Use one of your files and try the following code to see the effect between $line and "$line":

Code:
#!/bin/ksh
typeset line=""
typeset -i counter=0
cat <file> | while read line ; do
     print - "linenr $counter raw   : \"$line\""
     print - "linenr $counter cooked: " \"$line\"
     (( counter += 1 ))
done

bakunin
# 4  
Old 06-08-2005
Thanks bakunin,

But I need to use the second file, for each line of the first file. And, the line from the second file will be used in come other commands like cut and grep. Can you please help How can I proceed on this??
# 5  
Old 06-08-2005
Quote:
Originally Posted by kumariak
But I need to use the second file, for each line of the first file. And, the line from the second file will be used in come other commands like cut and grep.
I'm sorry, but I'm at a loss what you want.

Could you please bring a short example of what file1, file2 and the result should look like?

For starters, we had the theme "splitting a file into words" in the thread about "tokenizing", take a look there. This should solve your first file, which you want to read wordwise. The second file could be read by the cat | while ... done loop i sketched out for you already.

Once you have each of the desired values caught in variables you can do whatever you want.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Populating a BASH array with a list of files including spaces-in-the-name

For the record, I already tried telling mgmt and the users to disallow spaces in filenames for this script, but it isn't happening for a number of ID10T-error-based reasons. I have simple list of 3 files in a directory that are named like this: bash-3.2$ ls -1 file* file1 file1 part2... (2 Replies)
Discussion started by: ckmehta
2 Replies

2. UNIX for Dummies Questions & Answers

AWK print last field including SPACES

I have simple test.sh script, see below: bill_code=`echo $record | awk -F"|" '{print $1}'` Fullname=`echo $record | awk -F"|" '{print $3}'` email=`echo $record | awk -F\ '{print $4}'` The last field contains spaces: see csv below: A0222|Y|DELACRUZ|-cc dell@yahoo.com-cc support@yahoo.com ... (9 Replies)
Discussion started by: quay
9 Replies

3. Shell Programming and Scripting

how to read blank spaces

hi i have a file which store some data.the contents of my file is data1:data2 data3:data4 i have a script which read this file correct="$(cat /root/sh | cut -d: -f1)" i used this syntax..please help me which syntax is used to read blank spaces.and then remove it and after that how to read... (1 Reply)
Discussion started by: shubhig15
1 Replies

4. UNIX for Dummies Questions & Answers

How to read files with spaces

Hi I am a newbie to unix. I have a current script that reads a directory for excel files and renames the files. There is a problem now because some of the files have spaces. If I put quotes on the file, it will work but I dont know how to read all the files with quotes. Variables $1 =... (6 Replies)
Discussion started by: Lillyt
6 Replies

5. UNIX for Dummies Questions & Answers

Including files

Hi, Is it possible to include files (print with EOF, sort of like ssi) in perl/cgi? Thanks (1 Reply)
Discussion started by: marringi
1 Replies

6. Shell Programming and Scripting

PERL: including files

I am wondering how I can include external files in a perl script. I'm currently working on a website, and I'd like to put my menu items in a subroutine for example, and put that in another file such as menu.pl. That way, I can call the subroutine from each page (such as news.pl), and if I want to... (2 Replies)
Discussion started by: LNC
2 Replies

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

8. Shell Programming and Scripting

including spaces in awk output

I need to tweek my awk output: #cat filename ab cd ef:ghi:jk lm:nop qrs #cat filename | awk '{ for(i=3;i<NF+1;i++) printf $i}' ef:ghi:jklm:nopqrs I would like the ouput to include the original spaces from columns 3 on: ef:ghi:jk lm:nop qrs any suggestions? (4 Replies)
Discussion started by: prkfriryce
4 Replies

9. Shell Programming and Scripting

Keep spaces with read command

Hi The following command read a string from the keyboard & echo it back. $ read S;echo "$S" ABCD ABCD As you see, the input has space. while it echo back the spaces are removed. Is there a way to keep the input intact and not removing any spaces? Also, the number of spaces may vary. ... (3 Replies)
Discussion started by: azmathshaikh
3 Replies

10. Programming

Including lib files when compiling

I am using the C compiler included with AIX 4.3.3. I am unable to include libraries when I try to create the executable. Is there a switch that has to be used at compile time to make this happen?? Can anyone help?? (2 Replies)
Discussion started by: andrewl68
2 Replies
Login or Register to Ask a Question