i want it to print the common of the 2 file one file contain the ls -l option and the other one have the ls

ls only have the file name ls -l have everything including the permission and other info.

is there a way for them to print the output of the common one? the comm command doesnt seem to work ( it prints everything)

i tried grep -f file1 file2 but nothing shows up

lets say the ls -l outoutput is

-rwx------ 1 username 230 date and the filename

ls is

filname

i am trying to get the filename aka the union of the 2 file. I dont think there is a way for it to read the filename directory in shell

i want the

filename
link|improve this question

Can you please clarify what you're trying to do? Are you trying to find the union of filenames listed in both files? – daxelrod Nov 8 '11 at 0:49
@daxelrod modded the first post read above – ricedragon Nov 8 '11 at 0:53
feedback

2 Answers

up vote 1 down vote accepted
for i in ls-file; do grep -e $i$ ls-l-file; done

This loops through each line in ls-file (containing only the ls command) and assigns the output to $i. It then greps the ls-l-file (containing the ls -l command) and looks for a match, and outputs it.

Update

for i in ls-file; do grep -eo $i$ ls-l-file; done

The -o parameter will only output the matching text, so just the filename

Update 2

while read i; do  grep -oe $i$ ls-l-file; done < ls-file

This works better.

link|improve this answer
1  
Unfortunately, this won't work correctly if a filename is the same as the name of an owner or group of a file in ls-l-file, for example. – daxelrod Nov 8 '11 at 1:20
1  
Good point. I have edited to match only the end of a line – Paul Nov 8 '11 at 1:25
oh this works like a charm ,I been trying comm and diff ,and all sorts of grep command all day – ricedragon Nov 8 '11 at 3:07
feedback

You're part of the way there.

The first thing you need is to extract just the filenames from the file containing ls -l output, you can do so with

awk '{print $9}' < lsl_file

On my OS, the filename is the 9th column, it's possible it may be a different column on yours.

Note that comm requires both files to be sorted, this should already be the case with the output of ls and ls -l.

Therefore, your one-liner would be:

awk '{print $9}' < lsl_file | comm - ls_file

The single dash argument to comm tells it to use STDIN.

Important Assumption: this assumes that your filenames do not have spaces in them.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.