Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read file, but line is "cut in pieces" 1

Status
Not open for further replies.

peterve

IS-IT--Management
Mar 19, 2000
1,348
0
0
NL
I have a text file that contains multiple lines of text.

When i read the text file in a shell script, line by line, then the line appears to contain the individual words, separated by spaces

Code:
myfile.txt
-----------
John Doe 23 Street 1
Jane Doe 24 My Street Address 2


for myline in `cat myfile.txt`
do
  echo $myline
done


Output :
--------
John
Doe
23
Street
1
Jane
Doe 
24
My
Street
Address
2

I don't want the individual words, but I want the whole line in the $myline variable...

How can I do this ?

Note : The number of words per line is variable


--------------------------------------------------------------------
How can I believe in God when just last week I got my tongue caught in the roller of an electric typewriter?
---------------------------------------------------------------------
---------------------------------------------------------------
 
You could try using awk, for which I am not an expert, but something like this;

awk '{print $0}' <your_file_name>

This wil print each line in the file.

Hope this gets you on the way.
 
Hmmm If I replace 'cat <file>' with the awk command, the result is the same,

when I read the output, all 'lines' are treated as seperate words, instead of a line of words

I don't need to just ouput the lines, I need to be able to do manipulate each line... maybe that wasn't clear in my question

--------------------------------------------------------------------
How can I believe in God when just last week I got my tongue caught in the roller of an electric typewriter?
---------------------------------------------------------------------
---------------------------------------------------------------
 
Your script is doing exactly what you have programmed it to do ie. read the file and print it out word by word.

If you want to read the file line by line, try

Code:
while read myline
do
   echo $myline
done < myfile.txt

 
doesn't work either

I noticed that the words per line are separated by tabs... maybe that has something to do with it ?

How can I bypass that issue as well ?


--------------------------------------------------------------------
How can I believe in God when just last week I got my tongue caught in the roller of an electric typewriter?
---------------------------------------------------------------------
---------------------------------------------------------------
 
I beg to differ...

[tt]# cat tt
John Doe 23 Street 1
Jane Doe 24 My Street Address 2

# while read myline
> do
> echo $myline
> done <tt
John Doe 23 Street 1
Jane Doe 24 My Street Address 2
[/tt]



HTH,

p5wizard
 
the words are tab separated, and if I use the "while read myline" technique, the strings are returned as if they were space separated
I want to layout of the myline string to be exactly as it is in the file

any way to overcome that issue ?

--------------------------------------------------------------------
How can I believe in God when just last week I got my tongue caught in the roller of an electric typewriter?
---------------------------------------------------------------------
---------------------------------------------------------------
 
Code:
# while IFS= read myline
  do
     echo $myline
  done <tt

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
hmmm... still doesn't work

Maybe I need to give more specific info

Suppose you send the output of ps -ef to a file,
then the file is properly formatted into columns

When I read the file, I want every line from this file to keep the original column formatting
(so I can pick specific fields from the line based on the position in the string, using /bin/cut)

e.g. : user is position 1-8
process ID is position 10-14
and so on


Hope this clarifies my question a bit




--------------------------------------------------------------------
How can I believe in God when just last week I got my tongue caught in the roller of an electric typewriter?
---------------------------------------------------------------------
---------------------------------------------------------------
 
Hm...... works just fine here under Solaris/ksh:
Code:
ps -ef > /tmp/ps.txt

/tmp/ps.sh:
Code:
#!/bin/ksh
while IFS= read line
do
   echo "line->[${line}]"
done < /tmp/ps.txt
chmod +x /tmp/ps.sh
/tmp/ps.sh

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Furthermore... I'd suggest using 'awk/nawk' for the field-oriented inputs - you might find it easier to deal with and would provide you more leverage in dealing with the data.

Also if you insist on using 'cut', you don't have to specify character position - you can specify field separator and address the fields - 'man cut' for more details.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
It's the echo that converts the tabs into spaces, not the read...

[tt]
# cat tt
John Doe 23 Street 1
Jane Doe 24 My Street Address 2
# while read myline
do
echo [red]"[/red]$myline[red]"[/red]
done <tt
John Doe 23 Street 1
Jane Doe 24 My Street Address 2
[/tt]


HTH,

p5wizard
 
Good catch, p5wizard - I'm so used to using quotes....

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
that's it - plain and simple :)

thanks

--------------------------------------------------------------------
How can I believe in God when just last week I got my tongue caught in the roller of an electric typewriter?
---------------------------------------------------------------------
---------------------------------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top