command to extract sub-string out of file names


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers command to extract sub-string out of file names
# 1  
Old 04-28-2011
command to extract sub-string out of file names

I have these files in a directory. It may have more class than the sample below:
DEPT_CHEM101LEC_D_20110301.DAT
DEPT_CHEM101LAB_D_20110301.DAT
DEPT_BIO105LEC_D_20110325.DAT
DEPT_BIO105LAB_D_20110325.DAT
DEPT_CSC308LEC_D_20110327.DAT
DEPT_CSC308LAB_D_20110327.DAT

Is there way to extract out unique "sub-string" as show below in a temp file?

CHEM101
BIO105
CSC308

Thanks
# 2  
Old 04-28-2011
Code:
ls DEPT*.DAT |grep -Eo "[A-Z]+[0-9]+" |sort -u

This User Gave Thanks to rdcwayx For This Post:
# 3  
Old 04-29-2011
Thanks, It works well with scenarios in the sample. Now, some files have name without trailing numbers. that throw the matching partern off.

ex: DEPT_MUSICLAB_D_20110327.DAT

so i have to do it the tradditional way as

ls DEPT*.DAT | cut -f2 -d'_' | sort | uniq > tmp
sed -i 's/LAB//g' tmp ; sed -i 's/LEC//g' tmp

This will get leave just the name of the class at the end.
# 4  
Old 04-29-2011
Code:
# cat tst
DEPT_CHEM101LEC_D_20110301.DAT
DEPT_CHEM101LAB_D_20110301.DAT
DEPT_BIO105LEC_D_20110325.DAT
DEPT_MUSICLAB_D_20110327.DAT
DEPT_BIO105LAB_D_20110325.DAT
DEPT_CSC308LEC_D_20110327.DAT
DEPT_CSC308LAB_D_20110327.DAT

Code:
# while read a; do t=${a%???_*_*}; echo "${t#*_}"; done <tst
CHEM101
CHEM101
BIO105
MUSIC
BIO105
CSC308
CSC308

Code:
# sed 's/^[^_]*_//;s/..._.*//' tst
CHEM101
CHEM101
BIO105
MUSIC
BIO105
CSC308
CSC308


Last edited by ctsgnb; 04-29-2011 at 01:52 PM..
# 5  
Old 04-30-2011
Code:
ls DEPT*.DAT |awk -F _ '{sub(/LEC|LAB/,"",$2);a[$2]}END{for (i in a) print i}'

or
Code:
ls DEPT*.DAT |awk -F _ '{sub(/L[A-Z][A-Z]/,"",$2);print $2}' |sort -u

# 6  
Old 04-30-2011
Quote:
Originally Posted by ctsgnb
Code:
# cat tst
DEPT_CHEM101LEC_D_20110301.DAT
DEPT_CHEM101LAB_D_20110301.DAT
DEPT_BIO105LEC_D_20110325.DAT
DEPT_MUSICLAB_D_20110327.DAT
DEPT_BIO105LAB_D_20110325.DAT
DEPT_CSC308LEC_D_20110327.DAT
DEPT_CSC308LAB_D_20110327.DAT

Code:
# while read a; do t=${a%???_*_*}; echo "${t#*_}"; done <tst
CHEM101
CHEM101
BIO105
MUSIC
BIO105
CSC308
CSC308

Code:
# sed 's/^[^_]*_//;s/..._.*//' tst
CHEM101
CHEM101
BIO105
MUSIC
BIO105
CSC308
CSC308

thanks, eliminate sub-string with matching pattern will only work when the number of characters are not change. I end up using multiple sed to remove know keyword after the classes since there are more keywords than just LEC and LAB as show in sample. ex: LEC, LAB, ELECTIVE, MAJOR...
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extract file names from file and set variable to 'highest' one

HI Folks - I have a requirement where I need to scan a text file for a list of files. The file, we'll called it, files.txt looks like such: inbox/EBS/Client_GL_Detail_PBCS_112517_SEP2017.txt inbox/EBS/Client_GL_Detail_PBCS_112617_NOV2017.txt ... (9 Replies)
Discussion started by: SIMMS7400
9 Replies

2. UNIX for Beginners Questions & Answers

Question on grep command (extract a string)

Dear all, I have a file a.txt like below: 1_234560_A_G_b37 1 2 1 2 2 2 ... 1_35465767_C_T_b37 2 1 1 2 2 2 ... 2_490638010_A_T_b37 1 2 1 2 2 2 ... 10_4567899_T_G_b37 2 2 1 2 2 2 ... ... what I want to do is extracting rows starting with "10_" like : 10_4567899_T_G_b37 2 2 1 2 2... (1 Reply)
Discussion started by: forevertl
1 Replies

3. Shell Programming and Scripting

Command to extract word from a string

Hi All, I have a word and from that word would like to search for certain set of string, is there any command to do so ? EX : Components from the above word, would like to search for strings set and extract the search string and then do if stmt... pon nen ent Com say... (2 Replies)
Discussion started by: Optimus81
2 Replies

4. Shell Programming and Scripting

Get file names from string

Hello folks, would like as for your help. I'm downloading list of files which need to be downloaded later. Via curl I'm getting this output: % Total % Received % Xferd Average Speed Time Curr. Dload Upload Total Current Left ... (3 Replies)
Discussion started by: brusell
3 Replies

5. Shell Programming and Scripting

Grep string in files and list file names that contain the string

Hi, I have a list of zipped files. I want to grep for a string in all files and get a list of file names that contain the string. But without unzipping them before that, more like using something like gzcat. My OS is: SunOS test 5.10 Generic_142900-13 sun4u sparc SUNW,SPARC-Enterprise (8 Replies)
Discussion started by: apenkov
8 Replies

6. Shell Programming and Scripting

Extract file names from a file

I'm trying to extract a list of each .cfg file name mentioned in a file. I've made some progress using sed, but I'm still not there. Any help would be appreciated. My input looks like this: 07:00:00.000 spn redo 08:04:36.200 CMD OBJ_INIT 08:04:37.200 CMD OBJ_INIT ... (8 Replies)
Discussion started by: jricks
8 Replies

7. Shell Programming and Scripting

Extract a string between 2 ref string from a file

Hi, May i ask if someone share some command for extracting a string between 2 ref string in a txt file My objective: i had a file with multiple lines and wants only to extract the string "watch?v=IbkAXOmEHpY" or "watch?v=<11 random character>", when i used "grep 'watch?=*' i got a results per... (4 Replies)
Discussion started by: jao_madn
4 Replies

8. Shell Programming and Scripting

Extract all proper names from string with awk

I want to extract the proper names with awk from a very long string, like: õ(k): &lt;/span&gt;<br /><a something="pls/pe/person.person?i_pers_id=3694&amp;i_topic_id=2&amp;i_city_id=3372&amp;i_county_id=-1" target="_blank"><b>Gary Oldman</b></a> (George Smiley)<br /><a... (12 Replies)
Discussion started by: lyp
12 Replies

9. Shell Programming and Scripting

Search for string in a file and extract another string to a variable

Hi, guys. I have one question: I need to search for a string in a file, and then extract another string from the file and assign it to a variable. For example: the contents of the file (group) is below: ... ftp:x:23: mail:x:34 ... testing:x:2001 sales:x:2002 development:x:2003 ...... (6 Replies)
Discussion started by: daikeyang
6 Replies

10. Shell Programming and Scripting

command/script to extract a substring from a string

I have a long string "<ID type="Oid">{}</ID>" I need to extract "GigabitEthernet0/1" from the above string. How can it be done? :) (5 Replies)
Discussion started by: girisha
5 Replies
Login or Register to Ask a Question