foormatting file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers foormatting file
# 1  
Old 03-27-2008
Power foormatting file

UNIX for dummies...I need this.

Hello. I'm fairly new to UNIX. My boss gave me an option of using UNIX so I took the challenge, but I'm having problems.
I am accessing a directory ./Public and all of its subdirectories.
i.e
Code:
ls -l -R|awk '{print $1, "", $3, "", $9}'

because I only want to look at those fields. I have the data I want printing to a file dirlist.txt.
The problem is once I have the file, I want to organize it to show what each owner has (permissions on what file)
How can I begin parsing this file to look something like:
rx rx rx John ./Tool.pdf
x rw w ./Library.doc
blah ./blah
I wish to do this for each owner....

Any help would be greatly appreciated! Thanks
# 2  
Old 03-27-2008
The "something like" is not exactly crystal clear.

The permissions in the first three fields are fairly clear, although I would suggest you keep the dashes which the author of ls(1) wisely put there for a reason.

The second line doesn't have an owner field, is that intentional? If so, when should the owner be left out?

Does "I wish to do this for each owner" mean you want the output sorted by owner or is it just a corollary of the fact that you have owner information for all files, and want this done for each file?

The format you are using is not preserving any information about directory structure. You would need to apply awk only later, or modify the ls options so that you get a full path name for each file. (Or maybe your ls is different from mine. For one thing, I only get 8 fields with these options.)

How's this instead?

Code:
find . -type f -print |
perl -lne '@s = stat $_; printf "%04o %s %s\n", $s[2] & 0777, (getpwuid($s[4]))[0], $_'


Last edited by era; 03-27-2008 at 06:11 PM.. Reason: Add -type f to find
# 3  
Old 03-27-2008
I wish to sort the output file dirlist.txt by owner, yes. for each owner per directory, I want to list their permissions and files. The owner doesn't need to be left out, but I thought it may be easier to read. I'm thinking that once an owner is listed-just list the needed info for that owner.

I get 9 fields with the ls -l option.
-rw-r--r-- 1 tanya tanya 181038 25 Mar 17:30 sort.txt
drwxr-xr-x 5 tanya tanya 170 25 Nov 2006 workspace
I could not find the command to list the absolute path.

My textfile looks like...per directory.
./Documents:
drwxr-xr-x tanya Microsoft
drwxr-xr-x tanya meed

On the machine at work I'll have multiple owners so this is why I wish to organize by owner. The sort command seemed to help, but it did not sort by directory.

Hope this is more clear.. Thanks!
# 4  
Old 03-28-2008
For usability reasons, I would suggest you run find as it will give you the full path names. Parsing the output to the point where you can glue the directory path back on is not much fun, when you could get it for free.

Then you can post-process the output like I suggested; the little Perl script gives you the information you are looking for, and if you want to massage it into something less unreadable, there's where you'll get to play with awk.

Sorting on the second field (or fourth, if you insist on breaking up the permissions into three fields) should be easy after a cursory read of the sort(1) manual page (although it's tricky to get right on the first try -- sort is harder to use than it ought to be). Actually it's probably wise to sort the raw Perl output, and then if you like, post-process it with awk. The idea to break out the owner information and display it as a section heading instead sounds like a good one.

Here's what the Perl script prints:

Code:
find: ./gconfd-root: Permission denied
find: ./orbit-root: Permission denied
0600 era ./Tmp1.tmp-1.txt
0644 era ./fnord.sh
0644 era ./darcs.strace
0644 era ./nu
0644 era ./cut2
0600 era ./Tmp1.tmp.txt
0600 era ./tramp.6260Fvn
0600 era ./tramp.6260RND
0644 era ./orbit-era/bonobo-activation-server-ior
0700 era ./orbit-era/bonobo-activation-register.lock

(Only one owner here, but it's strictly in the order in which find(1) finds the files, so basically not sorted at all)

So if you want to map the permission bits to rwx symbols that's one thing left for you to do, after the sort by ownership.

Sorry about the confusion about the number of fields from ls(1) -- my locale has a different date format so the date is a single field for me (like 2008-03-28). With LC_ALL=C ls -l I get the same format as you. My bad.

Last edited by era; 03-28-2008 at 03:52 AM..
# 5  
Old 03-28-2008
Thanks for the input era. I do not quite understand the perl command you gave me since I know nothing of perl. Could you explain what it's doing?
Thanks for the advice
# 6  
Old 03-29-2008
It's basically a reimplementation of those parts of ls(1) which you need. The problem with ls is that its output is not particularly amenable to scripting.

So you take the file, stat() it, grab the permissions and the owner out of the stat results, and convert the owner from UID to name (getpwnam). But in the famous words of Dennis Ritchie, "You are not expected to understand this" (yet).
 
Login or Register to Ask a Question

Previous Thread | Next Thread

3 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script (sh file) logic to compare contents of one file with another file and output to file

Shell script logic Hi I have 2 input files like with file 1 content as (file1) "BRGTEST-242" a.txt "BRGTEST-240" a.txt "BRGTEST-219" e.txt File 2 contents as fle(2) "BRGTEST-244" a.txt "BRGTEST-244" b.txt "BRGTEST-231" c.txt "BRGTEST-231" d.txt "BRGTEST-221" e.txt I want to get... (22 Replies)
Discussion started by: pottic
22 Replies

2. Shell Programming and Scripting

Compare 2 text file with 1 column in each file and write mismatch data to 3rd file

Hi, I need to compare 2 text files with around 60000 rows and 1 column. I need to compare these and write the mismatch data to 3rd file. File1 - file2 = file3 wc -l file1.txt 58112 wc -l file2.txt 55260 head -5 file1.txt 101214200123 101214700300 101250030067 101214100500... (10 Replies)
Discussion started by: Divya Nochiyil
10 Replies

3. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies
Login or Register to Ask a Question