Finding the part of a filename


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Finding the part of a filename
# 1  
Old 08-21-2017
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:
Code:
libvclient_release_x64.so.740

and I must to find the number at the and of filename, which is in this case 740.

This number will change with time ( eg. it was a few days ago 734 ) so my ebuild must find it to works properly.

If I can to find this part of filename, then I can to store it into a variable which can then use further in the ebuild script.

The file path is known for my ebuild script but the filename can change, but only that number at the end of filename.

How can I find that part of the filename?

Best, Pál

Last edited by csanyipal; 08-21-2017 at 09:06 AM..
# 2  
Old 08-21-2017
Code:
specific_filename=libvclient_release_x64.so.740

number=${specific_filename##*[.]}

# 3  
Old 08-21-2017
Quote:
Originally Posted by rdrtx1
Code:
specific_filename=libvclient_release_x64.so.740

number=${specific_filename##*[.]}

It works,
Code:
echo ${number}

gives
Code:
740

But what I want is the following.
How can I find the number - in this case the 740 - if I do not know the full specific_filename?
Eg. I know only the 'libvclient_release_x64.so.' part of the filename, but do not know the last '740' part of it, and want to find that unknown part?
Code:
specific_filename=libvclient_release_x64.so.*

does not do the job.
# 4  
Old 08-21-2017
Hello csanyipal,

Welcome to forums, I hope you will enjoy learning and sharing knowledge here. Could you please try following and let me know if this helps you.
Code:
filename="libvclient_release_x64.so.740"
val=$(cut -d"." -f3 <<<$filename)
echo $val
740

EDIT: Adding this solution after seeing your POST#2, let me know if this helps you.
Code:
awk 'FNR==1{if(name){close(name)};split(FILENAME, a,".");print a[3];name=FILENAME;nextfile}' libvclient_release_x64.so.*

Thanks,
R. Singh

Last edited by RavinderSingh13; 08-21-2017 at 10:34 AM.. Reason: Adding this solution after seeing OP POST#2.
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 08-21-2017
Quote:
Originally Posted by RavinderSingh13
Hello csanyipal,

Welcome to forums, I hope you will enjoy learning and sharing knowledge here. Could you please try following and let me know if this helps you.
[/CODE] EDIT: Adding this solution after seeing your POST#2, let me know if this helps you.
Code:
awk 'FNR==1{if(name){close(name)};split(FILENAME, a,".");print a[3];name=FILENAME;nextfile}' libvclient_release_x64.so.*

Thanks,
R. Singh
It works, thank you.
I wonder if it works if the filname will change so, so the number at the and will change into four digit number too?
# 6  
Old 08-21-2017
Quote:
Originally Posted by csanyipal
It works, thank you.
I wonder if it works if the filname will change so, so the number at the and will change into four digit number too?
Hello csanyipal,

Glad that it helped you. You could HIT THANKS button at left most corner of each post if you find any post useful. Off course above will not work for every file name. Since you have provided pattern specific file patterns so I had written as per that file name(s). You may need to change the pattern of file(s) in case your name changes too.

Let me know if you have any queries on same.

Thanks,
R. Singh

Last edited by RavinderSingh13; 08-21-2017 at 10:51 AM..
This User Gave Thanks to RavinderSingh13 For This Post:
# 7  
Old 08-21-2017
Quote:
Originally Posted by RavinderSingh13
Hello csanyipal,

Welcome to forums, I hope you will enjoy learning and sharing knowledge here. Could you please try following and let me know if this helps you.
Code:
filename="libvclient_release_x64.so.740"
val=$(cut -d"." -f3 <<<$filename)
echo $val
740

EDIT: Adding this solution after seeing your POST#2, let me know if this helps you.
Code:
awk 'FNR==1{if(name){close(name)};split(FILENAME, a,".");print a[3];name=FILENAME;nextfile}' libvclient_release_x64.so.*

Thanks,
R. Singh
Quote:
Originally Posted by RavinderSingh13
Hello csanyipal,

Glad that it helped you. You could HIT THANKS button at left most corner of each post if you find any post useful. Off course above will not work for every file name. Since you have provided pattern specific file patterns so I had written as per that file name(s). You may need to change the pattern of file(s) in case your name changes too.

Let me know if you have any queries on same.

Thanks,
R. Singh
I am trying to use this in my ebuild:
Code:
lvcltno=awk 'FNR==1{if(name){close(name)};split(FILENAME, a,".");print a[3];name=FILENAME;nextfile}' \
           opt/VServer/vcomponents/libvclient_release_x64.so.*

dosym libvclient_release_x64.so."${lvcltno}" "${EPREFIX}/usr/$(get_libdir)/libvclient_release_x64.so"

but it does not work, the symlink is not created properly:
Code:
libvclient_release_x64.so -> libvclient_release_x64.so.

It seems that that the link is pointing to it self. Why? How can I fix this?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add part of directory name to filename

Hello, I need to add a part of folder name to the files inside it. For instance the file is HMCBackup_20150430.155027.tgz and it is under directory /nim/dr/HMCBackup/cops22 I need to add cops22 to the file name so as it would be cops22_HMCBackup_20150430.155027.tgz Any help in doing... (10 Replies)
Discussion started by: hasn318
10 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

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

7. Shell Programming and Scripting

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... (4 Replies)
Discussion started by: imonkey
4 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