Question about operating files
Hi guys, I am totally new to linux, and got a question with shell:
if there's a tab-delimited file under /usr/desktop, how can I determine the number of rows and columns of the file in shell? And, if told the the 3rd column of the file contains only numerical values and all values in the 5th column are unique, how can I verify these in shell? Thank you very much!!! |
You can use wc (wordcount) to get some of the information, for example rows (this would count the header row if there is one):
Code:
wc -l /usr/desktop/filename Code:
tail -1 /usr/desktop/filename | tr '\t' '\n' | wc -l Code:
for i in $(cat /usr/desktop/filename | cut -f3 -d' ' | grep -v "\D"); do grep -i $i /usr/desktop/filename; done Code:
cat /usr/desktop/filename | cut -f5 -d' ' | uniq -c | grep -v "\w*1" Also keep in mind you could perform most of these operations with perl, awk, sed, etc. easily also. |
Thank you very much for your help!!! Learning!
|
Quote:
Hi, maybe I am doing something incorrectly, but I these codes are not working as expected: The first one: Code:
wc -l /usr/desktop/filename The Second: Code:
tail -1 /usr/desktop/filename | tr '\t' '\n' | wc -l The third: Code:
for i in $(cat /usr/desktop/filename | cut -f3 -d' ' | grep -v "\D"); do grep -i $i /usr/desktop/filename; done The last one: Code:
cat /usr/desktop/filename | cut -f5 -d' ' | uniq -c | grep -v "\w*1" I am running this in the Mac Terminal, could this be the problem? Does this only work in linux? Thank you sooooooo much!!! |
my experience is that mac doesnt have all the default command line utilities you would normally find in a posix system.
can you please post the output of: Code:
which wc tail tr grep uniq cut cat also post the contents of the file and the exact error message you are receiving. |
You are replacing /usr/desktop/filename with the full path and name of the file you're attempting to examine correct?
On a mac the syntax may be slightly different, although I've not got one handy to test on at the moment, for the first one for example tyr: Code:
cat /usr/desktop/filename | wc -l |
Quote:
|
Are you hitting CTRL+V then TAB, or CMD+V ? On osx we're so wired to replace CTRL with CMD that we do it subconsciously sometimes
CTRL+V then TAB will insert a literal [TAB] keypress into the field. So if you're cutting and pasting his code into your terminal, you have to edit those ' ' spots to have a literal [TAB] keystroke inside of them. |
My understanding of mac text-files is that it uses yet another
method than Linux/Unix or DOS does for line endings. Code:
Linux = \LF believe there's many. Maybe try Code:
tr '\r' '\n' < /usr/desktop/filename | wc -l Cheers, Tink |
I thought that Mac OS X uses Linux (Unix) newlines, because it is Unix based.
|
In this case it really doesn't matter, it's translating tabs to newlines. not dos2unix or mac2dos or whatever.
|
Quote:
mac osX uses a bsd mach kernel which was originally the base of nextstep/openstep os which was developed by steve jobs after he left apple then incorporated into mac osX when apple bout out next and jobbs became part of apple and it is possible that even then that apple did someething custom |
Quote:
For the code Code:
which wc tail tr grep uniq cut cat Code:
$ which wc tail tr grep uniq cut cat Code:
1 11 a I ab Code:
0 /users/**/desktop/test.txt 2nd: Code:
32 Code:
8 88 h VIII hi |
most of these work for me:
Code:
[liveuser@localhost ~]$ uname -a -m -p not sure what the last 2 commands are supposed to do; as for the original post: Code:
[liveuser@localhost ~]$ wc -l test.txt for identifying numbers: man test google(regex) and for unique columns: man uniq you will need to know some basic bash to assemble it all together. |
All times are GMT -5. The time now is 12:29 PM. |