Extract unique filenames


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extract unique filenames
# 1  
Old 08-08-2011
Extract unique filenames

Hi Unix Gurus,

In a script, I am trying to extract unique text from a set of filenames.
I have certain files like below in a directory:
Code:
OPEN_INV_01012011.xls
OPEN_INV_01022011.xls
OPEN_INV_01032011.xls
CLOSE_INV_01012011.xls
CLOSE_INV_01022011.xls

I need to extract just "OPEN_INV_" and ""CLOSE_INV_"

I use the following command:
Code:
val=$(ls | sed 's/[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]\.[^.]*$//' | sort | uniq)
echo val: $val

This works fine. The last 8 characters before the file extension are always MMDDYYYY.

But when I have a file name with additional time stamp like:
OPEN_INV_01012011_1345.xls (here 01012011 is MMDDYYYY and 1345 is HHMM)
I am not able to get the file name part.

Please help me modify the "val" variable.

Thanks
Shankar

Last edited by Scott; 08-09-2011 at 02:05 PM.. Reason: Added code tags
# 2  
Old 08-08-2011
Just have it accept anything except digits.
Code:
$  echo OPEN_INV_01012011 | sed 's/\([^0-9]*\).*/\1/'
OPEN_INV_
$ echo OPEN_INV_01012011_1345.xls | sed 's/\([^0-9]*\).*/\1/'
OPEN_INV_
$

# 3  
Old 08-08-2011
Corona,

Thanks for the script. The code is fine and it works when the extensions are same.
But my requirement is some thing like below:
Code:
OPEN_INV_01012011.xls
OPEN_INV_01022011.xls
OPEN_INV_01012011.txt
OPEN_INV_01022011.txt

Even though the OPEN_INV_ will be same for .xls and .txt, I wanted to have distinction so that I can use them in my code.

Thanks

Last edited by Scott; 08-09-2011 at 02:05 PM.. Reason: Code tags
# 4  
Old 08-08-2011
Code:
$ echo OPEN_INV_01012011_1345.xls | sed 's/\([^0-9]*\)[^.]*\(.*\)/\1\2/'
OPEN_INV_.xls
$

# 5  
Old 08-08-2011
Thanks a bunch.

Last edited by shankar1dada; 08-08-2011 at 05:24 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract lines with unique value using a specific column

Hi there, I need a help with extracting data from tab delimited file which look like this #CHROM POS ID REF ALT Human Cow Dog Mouse Lizard chr2 3033 . G C 0/0 0/0 0/0 1/1 0/0 chr3 35040 . G T 0/0 0/0 ./. 1/1 0/1 chr4 60584 . T G 1/1 1/1 0/1 1/1 0/0 chr10 7147815 . G A 0/0 1/1 0/0 0/0... (9 Replies)
Discussion started by: houkto
9 Replies

2. Shell Programming and Scripting

Extract unique files

In a incoming folder i have list of files like below,i want to pick the unique files to process the job. if same file contain more than one then it should pick latest date modified file to process. drwxrwsrwx 2 n308799 infagrp 256 May 20 17:42 Final_Working drwxrwsrwx 2... (1 Reply)
Discussion started by: katakamvivek
1 Replies

3. Shell Programming and Scripting

Extract UNIque records from File

Hi, I have a file with 20GB Pipe Delimited file where i have too many duplicate records. I need an awk script to extract the unique records from the file and put it into another file. Kindly help. Thanks, Arun (1 Reply)
Discussion started by: Arun Mishra
1 Replies

4. UNIX for Dummies Questions & Answers

Extract unique combination of rows from text files

Hi Gurus, I have 100 tab-delimited text files each with 21 columns. I want to extract only 2nd and 5th column from each text file. However, the values in both 2bd and 5th column contain duplicate values but the combination of these values in a row are not duplicate. I want to extract only those... (3 Replies)
Discussion started by: Unilearn
3 Replies

5. Shell Programming and Scripting

Change unique file names into new unique filenames

I have 84 files with the following names splitseqs.1, spliseqs.2 etc. and I want to change the .number to a unique filename. E.g. change splitseqs.1 into splitseqs.7114_1#24 and change spliseqs.2 into splitseqs.7067_2#4 So all the current file names are unique, so are the new file names.... (1 Reply)
Discussion started by: avonm
1 Replies

6. Shell Programming and Scripting

Extract variables from filenames and output to file

I need some help. I have a list of files (thousands) and would like to extract some variables from the file name and save that to a file The list of files look like: I am trying to write the following script but I am stuck at how I can get thevariables 'doy' and 'yr' from each file and then... (5 Replies)
Discussion started by: malandisa
5 Replies

7. UNIX for Dummies Questions & Answers

Extract Unique Values from file

Hello all, I have a file with following sample data 2009-08-26 05:32:01.65 spid5 Process ID 86:214 owns resources that are blocking processes on Scheduler 0. 2009-08-26 05:32:01.65 spid5 Process ID 86:214 owns resources that are blocking processes on Scheduler 0. 2009-08-26... (5 Replies)
Discussion started by: simonsimon
5 Replies

8. Shell Programming and Scripting

extract unique pattern from large text file

Hi All, I am trying to extract data from a large text file , I want to extract lines which contains a five digit number followed by a hyphen , like 12345- , i tried with egrep ,eg : egrep "+" text.txt but which returns all the lines which contains any number of digits followed by hyhen ,... (19 Replies)
Discussion started by: shijujoe
19 Replies

9. Shell Programming and Scripting

How to extract date out of this filenames

I have filenames filenameA_fg_MMDDYY.tar.gz filenameASPQ_fg_MMDDYY.tar.gz filenameAFTOPHYYINGH_fg_MMDDYY.tar.gz filenameAGHYSW_fg_MMDDYY.tar.gz Is there a way I can extract the date out of these filenames? Thanks in advance (2 Replies)
Discussion started by: RubinPat
2 Replies

10. Shell Programming and Scripting

ksh scripting: Extract 1 most recent record for unique key

I'm loading multiple delimited files into an Oracle DB using sqlldr on Unix. I would like to get only the most recent record per each unique key. There may be multiple updates for each key, but I only want the most recent one. There is a date column in my delimited files, so I'm using cat to... (2 Replies)
Discussion started by: OPTIMUS_prime
2 Replies
Login or Register to Ask a Question

Featured Tech Videos