LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-11-2008, 10:02 PM   #1
VTGuitarMan
LQ Newbie
 
Registered: Mar 2008
Distribution: Debian, Ubuntu
Posts: 4

Rep: Reputation: 0
Storing part of a filename as a variable


Hi All,

I'm writing a simple bash shell script in which I need to store part of a filename as a variable. I have seen a number of posts about how to append an environment variable to a filename, but not the other way around. What I'd like to do is the following:

Using a for-loop structure, I want to extract numbers from filenames, i.e.:

file_1.txt
file_2.txt
file_3.txt
...
file_1000.txt

Each time I extract the number, I want to use it in a simple calculation, then discard the variable value in favor of the next file in the series. So, run a calculation using 1 as the value, then run calculation with 2, 3, etc., for the entire series of file_*.txt. I hope that makes sense.

Can someone point me in the right direction? I am figuring it might take some crafty use of sed or grep, or some such tool, but I haven't been able to suss it out for myself.

Thanks in advance!
 
Old 03-11-2008, 10:54 PM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
You can simply use variable expansion:

Code:
file=file_1.txt
temp=${file#file_}
num=${temp%.txt}
Or use sed:
Code:
file=file_1.txt
num=$(echo $file | sed 's/[^0-9]*\([0-9]*\)\.txt/\1/')
 
Old 03-11-2008, 11:15 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,119

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
Think I'd prefer
Code:
egrep -o '[[:digit:]]+'
instead of sed for something like this.
 
Old 03-11-2008, 11:24 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
#!/bin/sh
ls -1 file_* | sort -n -t"_" -k2 | while read -r fi; 
                                   do  
                                       echo ${fi##*_}
                                       num=${fi##*_}
                                       ## do calculation with $num
                                   done
 
Old 03-12-2008, 06:15 AM   #5
VTGuitarMan
LQ Newbie
 
Registered: Mar 2008
Distribution: Debian, Ubuntu
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks everyone for your quick replies and helpful input! I've been able to grab the variable from the files and do my calculations.
 
Old 03-12-2008, 06:47 AM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by syg00 View Post
Think I'd prefer
Code:
egrep -o '[[:digit:]]+'
instead of sed for something like this.
I'd have to agree with this since a failed pattern match in sed will give you the entire file name, whereas this will give you nothing if a match isn't made.
ta0kira
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using a variable containing a filename in grep TrumpetMan258 Programming 2 03-01-2008 01:27 PM
storing output of sed in a variable in shell script Fond_of_Opensource Linux - Newbie 1 11-09-2006 03:57 AM
how to use environment variable as part of filename beeblequix Linux - Newbie 3 07-11-2006 04:55 PM
storing octal value in char variable hubabuba Programming 1 04-17-2006 12:53 AM
put date as part of filename box_l Programming 3 07-27-2004 06:17 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration