ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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?.
$ 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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.