How to remove filenames having the same extension.?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove filenames having the same extension.?
# 1  
Old 08-07-2013
How to remove filenames having the same extension.?

hi,

i have a directory which contains some files and a subdirectory. i am writing only the files names to a file using the below code.
Code:
ls -ltr | grep "^-" | awk '{print $NF}' > /home/file_list$$

Code:
cat /home/file_list$$
s1_abc.txt
s2_def.xls
s3_def.xls

as you can see there is one .txt file and 2 .xls file.
i want to keep any one file for each extension. i.e i want to have unique file for each extension. so the
Quote:
file_list$$
will have only the following contents
Code:
s1_abc.txt
s2_def.xls

how can i do this?
# 2  
Old 08-07-2013
Hello,

Could you please use the following code.


Lets say here is the file with details.

Code:
$ cat remove_duplicate_extension
s1_abc.txt
s2_def.xls
s3_def.xls
s4_def.txs
s5_def.txt
s6.def.excel
s7.def.lxs


Here is the script for same.


Code:
 
$ cat remove_duplicate_extension.ksh
#value_check_b=0
while read line
do
a=`echo $line`
value_check_a=`echo $line | awk -F. '{print$2}'`
#echo $value_check_a
if [[ $value_check_a == $value_check_b ]]
then
echo ""
else
echo $line
fi
value_check_b=`echo $line | awk -F "." '{print$2}'`
done < "remove_duplicate_extension"


Output should be as follows.

Code:
$ ksh remove_duplicate_extension.ksh
s1_abc.txt
s2_def.xls
s4_def.txs
s5_def.txt
s6.def.excel

Please let me know if this helps.



Thanks,
R. Singh
# 3  
Old 08-07-2013
but in your output you are getting two .txt files.
i want a single file with each extension regardless of the file names.
# 4  
Old 08-07-2013
Code:
perl -e 'opendir(DIR , $ARGV[0]); chdir $ARGV[0]; while (readdir DIR ){($f)=$_=~/\.([^\.]+)$/;if ((-f $_ )&& (! defined $seen{$f})){$seen{$f}++; print "$_\n";}}' $DIRECTORY

# 5  
Old 08-07-2013
Code:
sed 's/\(.*\)\./\1%/g' file_list | sort -t'%' -u -k2 | tr '%' '.'

# 6  
Old 08-07-2013
sample below uses the same list as ravinder ... not as elegant as balajesuri's code but should be easy enough to understand ...
Code:
[root@centosgeek ~]# for ext in $(awk -F"[._]" '{print $NF}' testfile2 | sort -u);
> do
>     grep $ext testfile2 | head -1
> done
s6.def.excel
s7.def.lxs
s4_def.txs
s1_abc.txt
s2_def.xls
[root@centosgeek ~]#

# 7  
Old 08-07-2013
Code:
$ cat a.txt
s1_abc.txt
s2_def.xls
s3_def.xls
s4_def.txs
s5_def.txt
s6.def.excel
s7.def.lxs


$ awk -F\. '!a[$NF]++' a.txt
s1_abc.txt
s2_def.xls
s4_def.txs
s6.def.excel
s7.def.lxs

This User Gave Thanks to itkamaraj For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash to remove find and remove specific extension

The bash below executes and does find all the .bam files in each R_2019 folder. However set -x shows that the .bam extension only gets removed from one .bam file in each folder (appears to be the last in each). Why is it not removing the extension from each (this is $SAMPLE)? Thank you :). set... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

Remove extension in loop

Dear all sorry for bothering you wityh dumb question but I am stucked with an issue. Well, I am trying to loop over files in folder, make an operation and rewrite the output. Example: INPUT file1.txt file2.txt file3.txtMy command (doesn't work!!) for file in /path/to/*.txt do command... (13 Replies)
Discussion started by: giuliangiuseppe
13 Replies

3. Shell Programming and Scripting

Remove the file except with particular extension

Hi all i am new for the shell scripting can any one help me with my requirments . i want to delete file older than 21 days everything works fine but in that dir i got the files with should not be deleted with particular extension like (.info):confused:here is the script i wrote .can anyone... (5 Replies)
Discussion started by: vikatakavi
5 Replies

4. Shell Programming and Scripting

Remove spaces in filenames

Hi, I have files like below, In files coming as spaces. Before transfering those files into ftp server. I want to remove the spaces and then can transfer the files into unix server. e.g: filenames are 1) SHmail _profile001_20120908.txt 2) SHmail_profile001 _20120908.txt 3) sh... (3 Replies)
Discussion started by: kirankumar
3 Replies

5. Shell Programming and Scripting

change filenames but not extension

I have a filename with a bunch of periods that I want to replace with underscores, but I don't want to change the extension. Ex: I want file.test1.f-1.fig.eps to be file_test1_f-1_fig.eps Using awk, the following line will replace ALL periods with underscores, but I want to leave the... (2 Replies)
Discussion started by: erinbot
2 Replies

6. UNIX for Dummies Questions & Answers

removing the extension from all filenames in a folder

Hi there, I'm pretty new to UNIX and have tried trawling through this forum to find an answer to what I want to try to do, which I'm sure is very simple but I don't know how to do it. What I have a a folder that contains multiple files that I have copied from Windows and I want to remove the... (5 Replies)
Discussion started by: johnmcclintock
5 Replies

7. Shell Programming and Scripting

Reading filenames with extension .xml

Hi, I want to write a script to read all the filenames with extension .xml in a directory and pass the name of the file, one by one, to another function. Please help me out. Regards. Saurabh (3 Replies)
Discussion started by: bhalotias
3 Replies

8. Shell Programming and Scripting

remove file extension

Hi ALL, I'm new to this forum. Thanks and congrats to all for their great efforts building this site simply superb for all unix administrators. My requirement is to remove extensions of the files in the current directory. I'm doing it using below script which is working but i think it is... (12 Replies)
Discussion started by: prvnrk
12 Replies

9. Shell Programming and Scripting

Remove prefix from filenames

I'm trying to put together a shell script that will append specific prefixes based on the content of filenames. I think I have this part down. However, I want to append before that part a process that will remove the current prefix before it renames the files with the new prefix. For example,... (6 Replies)
Discussion started by: HLee1981
6 Replies

10. UNIX for Dummies Questions & Answers

Adding an extension to a group of filenames

Hi - I'm stuck. I have a group of text files created using the split command. My files have the names "projectaa", "projectab", "projectac", etc. What I want to do is add the extension ".txt" to each file. I think I've got part of a sed command together, but I'm stuck on my regex - I keep getting... (9 Replies)
Discussion started by: pepintheshort
9 Replies
Login or Register to Ask a Question