LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   how to find a file with uniq extension (http://www.linuxquestions.org/questions/programming-9/how-to-find-a-file-with-uniq-extension-777375/)

abhigrkist 12-22-2009 01:29 AM

how to find a file with uniq extension
 
Hi ,

I have one file called “ab” in below path. That file contains so many no of file with dot extensions.

cat ab
crank.ini
lion.DAT
tiger.bad
unix.txt
linux.ini
abhi.DAT
P.bad
del.bhabani
avatar.film
KNPH.film
sri.txt
suc.com
loh.txt
ama.com



Please tell me how can I knw only uniq .extensions??

catkin 12-22-2009 01:50 AM

What have you thought of and tried so far?

Your question is ambiguous. Do you mean that you are given extension ".ab" and you want to know if it is unique or you want to generate a list of extensions that are unique. By "unique" do you mean unique in the current directory, current file system, computer, network ...? The first step to solving a problem is to know exactly what the problem is!

deepinlife 12-22-2009 02:17 AM

Code:

#!/bin/bash

while read f ;
do
#        echo $f;
        echo ${f##*.} >> extensions
done <ab

sort extensions  | uniq -u -i>extension3

i think he needs to know the unique extensions in the file
extension3 will have the unique extensions,i think

ghostdog74 12-22-2009 02:35 AM

Code:

$ awk -F"." '!a[$NF]++{print $NF}' file
ini
DAT
bad
txt
bhabani
film
com


ashok.g 12-22-2009 02:38 AM

Can you explain the code??

ghostdog74 12-22-2009 03:16 AM

Code:

$ awk -F"." 'a[$NF]++{print $NF,a[$NF]}' file
ini 2
DAT 2
bad 2
film 2
txt 2
txt 3
com 2

the above says, store the extension in associative array "a", and increment its value each time its found and stored.

Code:

$ awk -F"." '!a[$NF]++{print $NF,a[$NF]}' file
ini 1
DAT 1
bad 1
txt 1
bhabani 1
film 1
com 1

By putting "!" operator in front, and since "!" precedence is higher than ++, it just says those extension that are "not" already in array, store it. Skip it if its already in array.


All times are GMT -5. The time now is 08:25 AM.