Ramdeo angh Ramdeo angh - 3 months ago 9
Bash Question

Parse File name in Bash script

I have a filename in a format like:

Master.csv-yyyy-mm-dd.txt
example:- Master.csv.2016-07-06.txt


I want substring from that in the following format.

YY-MM


please help me quickly

Answer

try this;

 ls M*.txt | awk -F. '{print substr($3,3,5)}'

user@user:/tmp$ ls M*.txt
Master.csv.2016-07-05.txt  Master.csv.2016-07-06.txt

user@user:/tmp$ ls M*.txt | awk -F. '{print substr($3,3,5)}'
16-07
16-07

if folder includes filename as hello_2016-07-06.txt, you can try this;

ls *.txt | grep -oP '[\d]+-[\d]+-[\d]+' | awk -F- '{print substr($1,3,2)"-"$2}'