Ramdeo angh Ramdeo angh - 1 year ago 99
Bash Question

Parse File name in Bash script

I have a filename in a format like:

example:- Master.csv.2016-07-06.txt

I want substring from that in the following format.


please help me quickly

Answer Source

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)}'

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}'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download