I want a shell script to get MMDDYYYY from the file with a name as mentioned below file
linuxbox.23566.MMDDYYYYHHMMSS.zip
Using bash string functions:
for file in *.zip; do
file="${file%.*}"
file="${file##*.}"
echo "${file:0:8}"
done
file="${file%.*}": Gets rid of the extension and stores the new name in file variablefile="${file##*.}": Gets rid of the longest match from beginning and stores the name in file variableecho "${file:0:8}": echoes the first 8 characters of whats left. $ ls
linuxbox.23566.MMDDYYYYHHMMSS.zip
$ for file in *; do file="${file%.*}"; file="${file##*.}"; echo "${file:0:8}"; done
MMDDYYYY
With cut:
$ cut -d. -f3 <<< "linuxbox.23566.MMDDYYYYHHMMSS.zip" | cut -c-8
MMDDYYYY
Because the first part is returning:
$ cut -d. -f3 <<< "linuxbox.23566.MMDDYYYYHHMMSS.zip"
MMDDYYYYHHMMSS
And then it gets the first 8 chars.