Getting part of a filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Getting part of a filename
# 1  
Old 07-22-2011
Getting part of a filename

Hi All,
I'm trying to get part of a filename and my skill with regular expression are lacking. I know I need to use SED but have no idea how to use it. I'm hoping that someone can help me out. The file names would be:

prefix<partwewant>suffix.extension
the prefix and suffix are always 3 characters and always the same [ABC, XYZ]. eg.

ABCN204867362XYZ.jpg
ABCC2039458XYZ.jpg
ABC2049583XYZ.jpg

Any ideas on how I can extract the middle part of the name?


Thanks in advance
# 2  
Old 07-22-2011
Code:
 
$ sed -e 's/.*ABC//' -e 's/XYZ.*//' test
N204867362
C2039458
2049583

$ cat test
ABCN204867362XYZ.jpg
ABCC2039458XYZ.jpg
ABC2049583XYZ.jpg

This User Gave Thanks to itkamaraj For This Post:
# 3  
Old 07-22-2011
Thanks heaps itkamaraj.
Based on your command, how can I add back the file extension?
# 4  
Old 07-22-2011
Using standard parameter expansion:

Code:
for f in ABC*XYZ.*; do
  _t=${f#???} _e=${_t#${_t%%???.*}}
  printf 'middle:\t%s\n' "${_t%$_e}"
  printf 'ext:\t%s\n' "${_e#???}"
done

Example output:


Code:
% ls -l
total 0
-rw-r--r-- 1 sysadmin None 0 Jul 22 11:41 ABC2049583XYZ.jpg
-rw-r--r-- 1 sysadmin None 0 Jul 22 11:42 ABCC2039458XYZ.png
-rw-r--r-- 1 sysadmin None 0 Jul 22 11:41 ABCN204867362XYZ.jpg
% for f in ABC*XYZ.*; do
  _t=${f#???} _e=${_t#${_t%%???.*}}
  printf 'middle:\t%s\n' "${_t%$_e}"
  printf 'ext:\t%s\n' "${_e#???}"
done  
middle: 2049583
ext:    .jpg
middle: C2039458
ext:    .png
middle: N204867362
ext:    .jpg

# 5  
Old 07-22-2011
Code:
$ sed -e 's/.*ABC//' -e 's/XYZ.*/.jpg/' test

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding the part of a filename

Hi, I am writing an ebuild for Gentoo Linux operating system. Writing an ebuild is about Bash scripting where I am a newbie. So, my ebuild must find a part of a specific filename. Such a filaname my look like this: libvclient_release_x64.so.740and I must to find the number at the and of... (18 Replies)
Discussion started by: csanyipal
18 Replies

2. UNIX for Dummies Questions & Answers

Replacing part of filename

Hi guys! I have quite a lot of files like all_10001_ct1212307460308.alf* and I want to get rid of the first number for all at once like: all_ct1212307460308.alf* How can I do this in the shell? (12 Replies)
Discussion started by: TimmyTiz
12 Replies

3. Shell Programming and Scripting

Extract a part of a filename containing a particular word

Hi All, Thanks in Advance Shell Script or Perl Script 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... (7 Replies)
Discussion started by: aealexanderraj
7 Replies

4. 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

5. 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

6. Shell Programming and Scripting

insert part of a filename in a textfile

Hi All, I've got a textfile that i've stripped and edited using sed and want to insert part of the filename (original filename consists of: fw0204.txt.ncat_report.txt) but there are multiple files being delivered by an application to the data directory The part of the filename that I need to... (0 Replies)
Discussion started by: etquart
0 Replies

7. Shell Programming and Scripting

cut the some part in filename

Hi All, I have the file & name is "/a/b/c/d/e/xyz.dat" I need "/a/b/c/d/e/" from the above file name. I tryning with echo and awk. But it not come. Please help me in this regard. Thanks & Regards, Dathu (3 Replies)
Discussion started by: pdathu
3 Replies

8. UNIX for Dummies Questions & Answers

Strip part from filename

I've many file like this 01-file 01_-_file 01_-_file 01_-_file 01_-_file 01-file I would remove bold part from filename. Suggestions?Thanks (4 Replies)
Discussion started by: cv313x
4 Replies

9. Shell Programming and Scripting

detecting the part of a filename

I like to have the date in the 2008-09-01 format at the beginning of my filenames. I then hyphenate after that and then have my filename. I have a script that creates this for me. However, I may be working on files that already have the date format already in there and so I don't want to have a... (4 Replies)
Discussion started by: mainegate
4 Replies

10. Shell Programming and Scripting

part of a filename

Hi, I need to extract only a part of the filenames of some files. The files are named this way : .tap_profile_SIT02 I want the "SIT02" part, which is not the same for each file. I was able to get what I want with bash, but not with ksh. Here is the command I used in bash : find... (8 Replies)
Discussion started by: flame_eagle
8 Replies
Login or Register to Ask a Question