Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.
print a file line-by-line
Name: Br1an Date: October 25, 2002 at 23:12:33 Pacific OS: Unix CPU/Ram: 256
Comment:
Hi, if i got a file "foo.txt": aaa bbb ddd aaa
and i want to print the file out line-by-line,then how to do that ? thanks a lot
Name: Br1an Date: October 25, 2002 at 23:13:37 Pacific
Reply:
...i mean in a script, if i want to read that file "foo.txt" and print out its contents line by line. Thanks
0
Response Number 2
Name: Sean Miller Date: October 26, 2002 at 01:33:59 Pacific
Reply:
I am assuming by "print" you mean "echo" ?? However, the rule would be the same for print -- see commented out line below.
The IFS sets the "input file seperator" which, by default, is space. What we're doing here is setting to be carriage return (ie. end of line). ^M means a physical ctrl-M (carriage return) -- in vi to do it use ctrl-V ctrl-M
#!/bin/ksh
IFS="^M"
for i in `cat foo.txt` do echo $i # echo $i | lp done
Summary: Hi, I wrote the following code to read a file line by line #!/bin/ksh for i in `cat a.txt` do echo $i done This is not showing me the file contents line bye line. Instead I am seeing a word by word ou...