I apologize for the confusing title, if you are coming in for a real cat, which meows and often plays innocently cute when they are under one-year old1, then this post has nothing to do with those devils who wear furs2.
A “cat”-like command or code is running but without using “cat” command, it’s what this post is about.
A couple of weeks ago, I suddenly wondered how many ways I can cat a file without using cat. Is there a simple command which only open file and dump to standard output? If you think cat is a simple command, then please man cat to see what it can do.
Be honest, how many of you have ever run cat file1 file2? I did twice, and that is probably all I have ever done for concatenating files. Standard input? Never did once. “Concatenating,” that’s what cat is named after, not for printing out a file that simple. Go check out the manpage, -n, -T, or -v options, have you used them before? cat isn’t that simple at all.
Alright, off-topic a little long, back to the subject. So far, I got these:
# Pure lame, period. echo "$(<filename)" printf '%s' "$(<filename)" # Or sed 's/grep/sed/' grep '' filename grep '' <filename # _Awk_ward... awk '{print $0}' filename # Head is tail, tail is head, what is what? head -n -0 filename tail -n +0 filename # Flip! Flip! Can we be dumber? tac filename | tac rev filename | rev # The best one to me, but it could be disaster if you forget to type # redirection < before filename. tee <filename # !@#$%^&* Serioius? OLDIDS="$IFS"; IFS=$'\n'; while read line; do echo "$line"; done <filename; IFS="$OLDIFS" # Pi... pie? python -c 'import sys; f=open(sys.argv[1]); sys.stdout.write(f.read()); f.close()' filename python -c 'import sys; sys.stdout.write(sys.stdin.read())' <filename
None of them are a good one. Any other ideas? or do you know if there is one common Linux command sits in my harddisk and it does just opening and printing?
[1] | Once they reach certain age, it’s like butterfly, turning into beautiful creatures from ugly wobble worms. Just, for cat, it might be the other way around. |
[2] | Actually, I like cat and I had a cat. He was a cutie, but I believe he often thought someone is going to murder him or someone owes him a million dollars. [2013-02-15T07:43:47Z: originally linked video was gone, I put up another one] |
I want to shed light on something I just found relating to this.
ReplyDeleteThe following benchmarks show that saving the contents of a file to a variable via read and echoing as opposed to simply echoing the file contents from a subshell as shown above takes nearly 1/2 the time! Very surprising to me..
# time for i in {0..1000};
> do (read var /dev/null;
> done;
real 0m0.596s
user 0m0.030s
sys 0m0.140s
# time for i in {0..1000};
> do (echo $(/dev/null;
> done;
real 0m0.969s
user 0m0.040s
sys 0m0.110s
those lines got sliced up..
Deletefast= read var </file/name && echo $var
slow= echo $(</file/name)