Extract a part of file name

Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Extract a part of file name
# 1  
Old 12-17-2007
Extract a part of file name


I want to extract a part of filename and pass it as a parameter to one of the scripts. Could someone help.

File name:-


Here i have to extract only XXXXX.XXXXXXX and the position will be constant. that means that i have to extract some n characters from xth position.

# 2  
Old 12-17-2007
s=`echo $f | cut -c 9-21`
echo "$f -> $s"

# 3  
Old 12-17-2007
echo "NLL_NAM_XXXXX.XXXXXXX_1_1.txt" | sed 's/_[0-9].*.*$//;s/^.*_//'

This User Gave Thanks to matrixmadhan For This Post:
# 4  
Old 12-17-2007
or in BASH:


# ${stringSmilieosition:length}
extract=${$filename:9:13} # nb. no spaces around the colons

echo $extract

see http://tldp.org/LDP/abs/html/refcards.html#AEN20417
in the advanced bash scripting guide.
# 5  
Old 12-17-2007
Thank you all.

I just used this code and it worked:-

s=`echo $f | cut -c 9-21`
echo "$f -> $s"
# 6  
Old 12-17-2007
The solutions that have been suggested will work fine, so long as the length of the filename does not vary. If it does, then you need a different solution.

One possibility is this:

f1=${f%%.*} # Extracts part of name before first period (NLL_NAM_XXXXX)
f2=${f#*.} # Extracts part of name after first period (XXXXXXX_1_1_.txt)

The generation of fileName deserves a little explanation. What I have done is split the filename into the piece before the first period and the piece after. So the first piece (f1) contains the name up to XXXXX and the second piece (f2) contains the piece from XXXXXXX on. Next I strip the part of f1 up to and including the last underscore (${f1##*_}, leaving XXXXX, concatenate with a period (.) and what is left of f2 after removing everything from the first underscore on (${f2%%_*}. The resulting string (XXXXX.XXXXXXX) is assigned to the variable fileName.

This solution will work in place of the other solutions that rely on extracting a fixed length segment of srting starting a a fixed position. The difference is that the dependence on length is eliminated. The only assumption made about the string is that there is no period before the XXXXX.XXXXXXX in the original filename.
# 7  
Old 12-18-2007
Although Mark's is an elegant solution, there's a simpler way to do and still remain independent of filename length: use cut's field delimiter option:

s=`echo $f | cut -d "_" -f3`
echo "$f -> $s"

Here, we tell cut to use underscore ("_") as the field delimiter. cut now sees the string as five fields (NLL, NAM, XXXXX.XXXXXXX, 1 and 1.txt respectively) and spits out the third.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extract a part of variable/line content in a file

I have a variable and assigned the following values ***XYZ_201519_20150929140642_20150929140644_211_0_0_211 I need to read this variable from backward and stop read when I get first underscore (_) In this scenario I should get 211 Thanks Kris (3 Replies)
Discussion started by: mkris
3 Replies

2. Programming

Extract part of an archive to a different file

I need to save part of a file to a different one, start and end offset bytes are provided by two counters in long format. If the difference is big, how should I do it to prevent buffer overflow in java? (7 Replies)
Discussion started by: Tribe
7 Replies

3. UNIX for Advanced & Expert Users

Need help to extract part of the string

Hi, I have a string with name as 20140412-s1-Potopolive_promos_20140412. So I want to extract only Potopolive string. Could you please help me the command. O/p : Potopolive Thx in advance (5 Replies)
Discussion started by: lkeswar
5 Replies

4. Shell Programming and Scripting

Extract the part of sequences from a file

Discussion started by: rahim42
8 Replies

5. Shell Programming and Scripting

Extract part of file

Hello All, I need to extract part of a file into a new file My file is Define schema xxxxxx Insert into table ( a ,b ,c ,d ) values ( 1, 2, 3, (15 Replies)
Discussion started by: nnani
15 Replies

6. Shell Programming and Scripting

Extract part of a string

I have a file with 100s of lines of text. I want to perform an extraction of this line: Info bpzs(pid=2652) using 1000 bits I have not been able to extract it. Should I try expr match or is there another method? This line has data both in front of and in back of it. I do not have grep -o... (5 Replies)
Discussion started by: newbie2010
5 Replies

7. Shell Programming and Scripting

how to extract a certain part of a line

Hi friends, I want to select and use the certain part of a line. For example I have the following line home/1245/hgdf/acsdf/myhome/afolder/H2O/endfile how can I extract the part " /myhome/afolder/H2O/endfile " thanks (6 Replies)
Discussion started by: rpf
6 Replies

8. Shell Programming and Scripting

extract part of text file

I need to extract the following lines from this text and put it in different files. From xxxx@gmail.com Thu Jun 10 21:15:46 2010 Return-Path: <xxxxx@gmail.com> X-Original-To: xxx@localhost Delivered-To:xxxx@localhost Received: from ubuntu (localhost ) by ubuntu (Postfix) with ESMTP... (11 Replies)
Discussion started by: waxo
11 Replies

9. Shell Programming and Scripting

extract last part of string.

Hi, I have a string like this BUNDLE=/home/bob/flx/user.bun how to extract only the the last part ie, only user.bun (2 Replies)
Discussion started by: vprasads
2 Replies

10. Shell Programming and Scripting

How to extract certain part of log file?

Hi there, I'm having some problem with UNIX scripting (ksh), perhaps somebody can help me out? For example: ------------ Sample content of my log file (text file): -------------------------------------- File1: .... info_1 ... info_2 ... info_3 ... File2: .... info_1 ... info_2 ...... (10 Replies)
Discussion started by: superHonda123
10 Replies
Login or Register to Ask a Question

Featured Tech Videos