LinuxQuestions.org
Register a domain and help support LQ
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
 
LinkBack Search this Thread
Old 08-11-2009, 11:07 PM   #1
kicap
LQ Newbie
 
Registered: Nov 2004
Posts: 3

Rep: Reputation: 0
Read filename as a variable


I am really2 appreciate if someone could gave me some idea. I have a file name '2009-08-06_00-00-00.csv' and how could i produce an output as '2009-08-06' that yield from the given filename by using shell script?.

Thanks
 
Old 08-12-2009, 12:10 AM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 1,997

Rep: Reputation: 107Reputation: 107
If you care about the first 10 characters:
Code:
echo $FILENAME | cut -b 1-10
If you care about everything before the _
Code:
echo $FILENAME | sed s/_.*//
 
Old 08-12-2009, 02:10 AM   #3
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: Fedora
Posts: 220

Rep: Reputation: 43
basename file.csv .csv

will give you the correct result (man basename)
oops didn't read the question properly did I?

Last edited by ArfaSmif; 08-12-2009 at 08:19 PM. Reason: added comment
 
Old 08-12-2009, 04:27 PM   #4
David the H.
Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 5,339

Rep: Reputation: 1202Reputation: 1202Reputation: 1202Reputation: 1202Reputation: 1202Reputation: 1202Reputation: 1202Reputation: 1202Reputation: 1202
Using no eternal tools, only bash built-ins:
Code:
$ filename="2009-08-06_00-00-00.csv"

$ echo ${filename%_*}  #remove everything from the last _ to the end
2009-08-06

$ echo ${filename#*_}  #remove everything from the beginning to the first _
00-00-00.csv

$ echo ${filename%.*}  #remove everything from the last . to the end
2009-08-06_00-00-00

$ echo ${filename#*.}  #remove everything from the beginning to the first .
csv

$ echo ${filename%%-*} #remove everything from the first - to the end
2009

$ echo ${filename##*-} #remove everything from the beginning to the last -
00.csv

$ echo ${filename:5:2}  #from position 5, print 2 characters
08

$ echo ${filename/00/11}  #replace the first 00 with 11
2119-08-06_00-00-00.csv

$ echo ${filename/_00/_11}  #replace the first _00 with _11
2009-08-06_11-00-00.csv

$ echo ${filename//-/_}  #change all - to _
2009_08_06_00_00_00.csv
Parameter substitution is a wonderful thing.

Last edited by David the H.; 08-13-2009 at 01:29 PM. Reason: 1st: Some changes and additions. 2nd: fixed an error
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script to read part of filename fatgoth Linux - Newbie 10 02-26-2012 08:21 AM
Storing part of a filename as a variable VTGuitarMan Programming 5 03-12-2008 06:47 AM
Using a variable containing a filename in grep TrumpetMan258 Programming 2 03-01-2008 01:27 PM
how to use environment variable as part of filename beeblequix Linux - Newbie 3 07-11-2006 04:55 PM
read filename from directory newbielinux Linux - Newbie 2 03-02-2004 03:48 PM


All times are GMT -5. The time now is 05:08 AM.

Main Menu
 
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
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration