Help filter content of a file

Hi all !

I have a file name file1 like this :

/A
/A/1
/A/2
/B
/B/3
/B/4
/tmp/C
/tmp/C/5
/tmp/C/6

I want to write a script to take content from file2 and print out to file2 only these lines :

/A
/B
/tmp/C

Can someone help ?

grep -v '[0-9]$' file1
grep '[A-Z]$' file1

Lolz thank to jim mcnamara and xoops. Sure it works. But Im sorry that not what I mean. Number and alphabet are just an example cause I was lazy. What I meant is there's a list of parent directories and sub directories inside them. I want to list out only the parents. Here's another one :

/Cars
/Cars/Toyota
/Cars/Ford
/Planes
/Planes/Airbus
/Planes/Boeing
/Old/Ships
/Old/Ships/Titanic

I want to print out :

/Cars
/Planes
/Old/Ships

try;

while read dir; do dirname $dir; done < file1 | sort | uniq

Here the result :

/
/Cars
/Old
/Old/Ships
/Planes

Thanks but I dont want "/" and "/Old" printed out

But this will list the directories / and /Old as well.

I was aware about the result.
But those are also the parent dir as you mentioned.

Do you have these fix set of dir? if yes then of course you can format the output. but if those are just the example then what is the rule behind printing of parent dir?

Try:

awk '$0~p"/"{print p}{p=$0}' infile

@anchal_khare: It's just an example. The rule is if there are a line /dir1/dir2 and other lines which the same as first line plus more "dir*" /dir1/dir2/dir3a, /dir1/dir2/dir3b or /dir1/dir2/dir3a/dir4... then only print /dir1/dir2

---------- Post updated 01-15-10 at 08:51 AM ---------- Previous update was 01-14-10 at 10:11 PM ----------

@Scrutinizer: If I add only one more line /Bicycle (mean it doesn't have sub dir) then it will not be printed out. Do you have another solution ? Thanks

Anyone help ? :frowning:

sort file | awk '$0 !~ var || NR == 1 {  var=$0; print; } '

Thanks anbu23 ! It works well :slight_smile: