Limitations of tac/cat?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Limitations of tac/cat?
# 1  
Old 10-04-2010
Limitations of tac/cat?

As part of a quiz assigned during my unix class I was asked to write a program to ask for a file name, print read errors, and "reverse elements in a list."

I used the 'tac' command in my solution, however, I was then lectured for 5 min about the "limitations" of the 'tac' command and how a 'for' loop would have been more robust. During which I was also told such a command wouldn't work with a "one million" line text file.... is this true?, and if so is it really relevant issue?

my code:
Code:
#!/bin/bash
echo -n "Enter file name: "; read path;
tac $path > $"output.list" || echo "Error!"

# 2  
Old 10-04-2010
Quote:
Originally Posted by 127.0.0.1
...I was also told such a command wouldn't work with a "one million" line text file.... is this true?, ...
It's easy to check that that's not true.

Code:
$
$ # create a million-line file
$ perl -le 'for (1..1_000_000){print "this is line $_"}' >file_1mil.txt
$
$ wc file_1mil.txt
 1000000  4000000 20888896 file_1mil.txt
$
$
$ time tac file_1mil.txt
this is line 1000000
this is line 999999
this is line 999998
this is line 999997
this is line 999996
this is line 999995
this is line 999994
this is line 999993
this is line 999992
this is line 999991
this is line 999990
...
...
...
this is line 10
this is line 9
this is line 8
this is line 7
this is line 6
this is line 5
this is line 4
this is line 3
this is line 2
this is line 1
 
real    4m9.095s
user    0m7.186s
sys     0m21.328s
$
$

It does work with a million lines and I think, is pretty fast.

tyler_durden
# 3  
Old 10-04-2010
My main concern would be that tac would read the whole file into memory before reversing it, which has obvious limits. I tried it with strace and found that GNU tac, at least, does no such thing. It seeks to EOF-8K and reads 8K chunks, seeking upwards in 8K jumps. It might have a line-size limit of 8K, but it can handle files of arbitrary size.

Of course, doing so requires seeking, which it can't do if the input is a pipe. In that case it creates a temporary file which it cat's the input into, then backward-reads through the temp file as described above. So for piped streams it may be limited by disk space in /tmp.

Last edited by Corona688; 10-04-2010 at 06:42 PM..
These 2 Users Gave Thanks to Corona688 For This Post:
# 4  
Old 10-04-2010
thanks for the quick responses, i will be sure to explain how the gnu version of tac we are using works to the grad student teaching the class.
# 5  
Old 10-05-2010
Quote:
Originally Posted by 127.0.0.1
As part of a quiz assigned during my unix class I was asked to write a program to ask for a file name, print read errors, and "reverse elements in a list."

I used the 'tac' command in my solution, however, I was then lectured for 5 min about the "limitations" of the 'tac' command and how a 'for' loop would have been more robust.
I think it's not an issue question but that you were asked to write a program and show what logic you would involve (in state of using the "out of the box" tac).
Don't you think so ?
# 6  
Old 10-05-2010
#deleted#

---------- Post updated at 01:33 AM ---------- Previous update was at 01:29 AM ----------

Code:
file=$(<$path)
for((i=${#file};i>0;i--)); do  printf "${file:$i:1}"; done

# 7  
Old 10-06-2010
Quote:
Originally Posted by frans
I think it's not an issue question but that you were asked to write a program and show what logic you would involve (in state of using the "out of the box" tac).
Don't you think so ?
The instructor tried to cover that by telling him nonsense instead, and should be called on that IMHO Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX Desktop Questions & Answers

Cat/tac displays nothing but vi does

Hi, Today i have seen a file which shows nothing when i use cat/tac. But when i use vi it displays a single line at the top. There are no other lines. I am curious why cat/tac doesn't display that line and only vi does? Any thoughts? Thanks in advance, (1 Reply)
Discussion started by: pandeesh
1 Replies

2. Shell Programming and Scripting

Want tic tac toe project in shell...

Any body pls provide me source code of tic tac toe in shell.... (1 Reply)
Discussion started by: Pushpender Sing
1 Replies

3. Solaris

Solaris limitations

Hi, I recently started working with Solaris, and what I noticed is that a lot of commands I used to regularly use don't work, like sed -i and grep -r. I have found work arounds for these problems though but it's a pain in the ass. I'm just wondering why they decided not to include these handy... (4 Replies)
Discussion started by: Subbeh
4 Replies

4. UNIX and Linux Applications

gnuplot limitations

I'm running a simulation (programmed in C) which makes calls to gnuplot periodically to plot data I have stored. First I open a pipe to gnuplot and set it to multiplot: FILE * pipe = popen("gnuplot", "w"); fprintf(pipe, "set multiplot\n"); fflush(pipe); (this pipe stays open until the... (0 Replies)
Discussion started by: sedavidw
0 Replies

5. Shell Programming and Scripting

cat in the command line doesn't match cat in the script

Hello, So I sorted my file as I was supposed to: sort -n -r -k 2 -k 1 file1 | uniq > file2 and when I wrote > cat file2 in the command line, I got what I was expecting, but in the script itself ... sort -n -r -k 2 -k 1 averages | uniq > temp cat file2 It wrote a whole... (21 Replies)
Discussion started by: shira
21 Replies

6. UNIX for Dummies Questions & Answers

Difference between cat , cat > , cat >> and touch !!!

Hi Can anybody tell the difference between Difference between cat , cat > , cat >> and touch command in UNIX? Thanks (6 Replies)
Discussion started by: skyineyes
6 Replies

7. UNIX for Dummies Questions & Answers

Password limitations.

I would like to set my minimum password length to on Linux and AIX. However, doing this normally would only make it so newly added users will be affected by this. I would like for when I make this change, it either truncates everyone elses password, or prompts them to change it to 8+ characters.... (2 Replies)
Discussion started by: syndex
2 Replies

8. Programming

tic tac AHHHHHHHHHHHHHHHHH

ok i am looking for a vim coad to make a tic tae toe game (dont asky why) can any one give me a URL of help :confused: :confused: :confused: (0 Replies)
Discussion started by: thepicoman
0 Replies

9. UNIX for Dummies Questions & Answers

mkdir limitations

What characters can't be used with a mkdir? Any limits on length of name? Thank you, Randy M. Zeitman http://www.StoneRoseDesign.com (12 Replies)
Discussion started by: flignar
12 Replies
Login or Register to Ask a Question