Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to print the common of the 2 file

One file contain the ls -l option and the other one have the ls option

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

Is there a way for them to print the output of the common one? The comm command doesn't seem to work (it prints everything)

I tried grep -f file1 file2 but nothing shows up

lets say the ls -l output 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 don't think there is a way for it to read the filename directory in shell

I want the

filename
share|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
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.

share|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

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.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .