How to extract when filename contains file seperator..?

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to extract when filename contains file seperator..?
# 1  
Old 04-09-2018
How to extract when filename contains file seperator..?

Hi,
I want to extract part of filename, for eg:

Code:
File="010020004_S-TOR-Sort-CASAP_20170519_121504_0007.TXT"

here i need first 5 words of file i.e.

Code:
FilePart="$(echo "${File%[-_]"${File#*[_-]*[-_]*[_-]*[_-]*[_-]*}"}")"
Echo $FilePart
010020004_S-TOR-Sort-CASAP

But what if i get filename like below:

Code:
File="010020004_S-TOR-Sort-CAS_P_20170519_121504_0007.TXT"
FilePart="$(echo "${File%[-_]"${File#*[_-]*[-_]*[_-]*[_-]*[_-]*}"}")"
Echo $FilePart
010020004_S-TOR-Sort-CAS

So in second file filename contains [_-] as seperators but "_" is present in filename itself..is there any way i can skip specific seperator and ignore if "_' is not seperator.

Kindly help me out on this
TIA
# 2  
Old 04-09-2018
Assuming your filenames always have the _yyyymmdd_hhmmss_vvvv.TXT component and your shell is bash 4.x, try
Code:
FilePart=${File:0:-25}

or for bash 3.x:
Code:
FilePart=${File:0:(( ${#File}-25 ))}

Andrew

Last edited by apmcd47; 04-09-2018 at 07:34 AM.. Reason: typos!
# 3  
Old 04-09-2018
If you state that _ and - are word separators and you want the first 5 words from the string contained in a variable; no your code cannot reliably guess at how many separators in the first 5 words are not separators.

If you know that the part of the filename that you do not want always ends with three occurrences of _string, you could use:
Code:
File="010020004_S-TOR-Sort-CAS_P_20170519_121504_0007.TXT"
FilePart=${File%_*_*_*}
echo "$FilePart"

with any shell that performs the variable expansions required by the POSIX standards.

Note also that there is a huge difference between using the command name echo and the command name Echo in any system that conforms to the standards. Utility names are case sensitive and there is no definition in the standards as to what a command named Echo will do (if such a command exists on your system).

Also note that there is no need to use echo and command substitution in the assignment of the variable expansion.

Last edited by Don Cragun; 04-09-2018 at 07:53 AM.. Reason: Add second note.
# 4  
Old 04-09-2018
If you know for sure that the last subitem you want has alphabetic letters, and the first you want removed has digits, try
Code:
File="010020004_S-TOR-Sort-CASAP_20170519_121504_0007.TXT"
echo ${File%_[0-9]${File#*[_-]*[_-]*[_-]*[_-]*[^A-Z_]}}
010020004_S-TOR-Sort-CASAP
File="010020004_S-TOR-Sort-CAS_P_20170519_121504_0007.TXT"
echo ${File%_[0-9]${File#*[_-]*[_-]*[_-]*[_-]*[^A-Z_]}}
010020004_S-TOR-Sort-CAS_P

If you can't rely on that structure, I'm afraid you're lost.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract data according to keys from filename mentioned in file

Hello experts, I want to join a file with files whosE names are mentioned in one of the columns of the same file. File 1 t1,a,b,file number 1 t1,a,c,file number 1 t2,c,d,file number 2 t2,c,e,file number 2 t2,c,f,file number 2 t2,c,g,file number 2 t3,e,f,file number 3 file number 1... (3 Replies)
Discussion started by: ritakadm
3 Replies

2. Shell Programming and Scripting

Extract string from filename

Hi I need to extract the string from file name filename: FILENAME_STRUT_01032013_XXXXXXX.TXT I want 01032013 from the above file name. number of characters may differ before the required string but underscores(-) are same number i.e. after second underscore. Please advise on this. ... (2 Replies)
Discussion started by: cnrj
2 Replies

3. Programming

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My code: if then set "subscriber" "promplan" "mapping" "dedicatedaccount" "faflistSub" "faflistAcc" "accumulator"\ "pam_account"; for i in 1 2 3 4 5 6 7 8;... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

4. UNIX for Dummies Questions & Answers

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My Requirement: 1) There are some set of files in a directory like given below OTP_UFSC_20120530000000_acc.csv OTP_UFSC_20120530000000_faf.csv OTP_UFSC_20120530000000_prom.csv... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

5. Shell Programming and Scripting

extract every filename containing certain string in a directory and do some commend per file

Hi, Here is my question: suppose I have files like 1990_8xdaily_atmos.nc 1991_8xdaily_atmos.nc 1992_8xdaily_atmos.nc 1993_8xdaily_atmos.nc 1990_daily_atmos.nc 1991_daily_atmos.nc 1992_daily_atmos.nc 1993_daily_atmos.nc 1990_month_atmos.nc 1991_month_atmos.nc 1992_month_atmos.nc... (1 Reply)
Discussion started by: 1988PF
1 Replies

6. Shell Programming and Scripting

Extract date from filename and create a new file

Hi, i have a filename CRED20102009.txt in a server 20102009 is the date of the file ddmmaaaa format the complete route is /dprod/informatica/Fuentes/CRED20102009.csv i want to extract the date to create a new file named Parameters.txt I need to create Parameters.txt with this... (6 Replies)
Discussion started by: angel1001
6 Replies

7. UNIX for Dummies Questions & Answers

Extract first line of a file and use as filename

I am trying to find a way to create a script which will extract the first line of a file and then rename the file (or create a new file with the same content as the old file) using the first line as the name. The first line being a single word, that is. I am hopeless at programming, if anyone can... (5 Replies)
Discussion started by: s.plumb
5 Replies

8. Shell Programming and Scripting

How to extract timestamp from the filename?

Hi., My file name is of the format: name_abc_20100531_142528.txt where., my timestamp is of the format: yyyymmdd_hhmmss How to extract the date strring and time string into seperate variables in the shell script, after reading the file as the input? I want to get the variables... (9 Replies)
Discussion started by: av_vinay
9 Replies

9. Shell Programming and Scripting

Add a field seperator in a file.

"355"|""|"NJ"|"A0A 1W0"|"V"|""|""|""|"N" I've the above sample data seperated with pipe delimeter and in the file I want to replace a space with "|" to the 4th field so the result would be like below. So it would change from 9 fields to 10 fields. "355"|""|"NJ"|"A0A"|"1W0"|"V"|""|""|""|"N" ... (3 Replies)
Discussion started by: rudoraj
3 Replies

10. UNIX for Dummies Questions & Answers

extract only filename

Hi, I want to display only the file names of the ls command with a delimiter of comma. For example:- ls /etc/dir/N* gives the following output /etc/dir/N1 /etc/dir/N2 /etc/dir/N3 I want the output as N1,N2,N3,-->Here i need the final comma also. Also, here /etc/dir is being... (2 Replies)
Discussion started by: dnat
2 Replies
Login or Register to Ask a Question