I assume that there may be a better way to do it but the only one I came up with was using AWK.
I have a file with name convention like following:
testfile_2016_03_01.txt
Using one command I am trying to shift it by one day testfile_20160229.txt
I started from using:
file=testfile_2016_03_01.txt IFS="_" arr=($file) datepart=$(echo ${arr[1]}-${arr[2]}-${arr[3]} | sed 's/.txt//') date -d "$datepart - 1 days" +%Y%m%d the above works fine, but I really wanted to do it in AWK. The only thing I found was how to use "date" inside AWK
new_name=$(echo ${file##.*} | awk -F'_' ' { "date '+%Y%m%d'" | getline date; print date }') echo $new_name okay so two things happen here. For some reason $4 also contains .txt even though I removed it(?) ##.*
And the main problem is I don't know how to pass the variables to that date, the below doesn't work
`awk -F'_' '{"date '-d "2016-01-01"' '+%Y%m%d'" | getline date; print date}') ideally I want 2016-01-01 to be variables coming from the file name $2-$3-$4 and substract 1 day but I think I'm getting way too many single and double quotes here and my brain is losing..
${file%%.*}instead of##(or just one%as there is just one.anyway).##removes matching from the start, you'd use it to get, e.g., an extension or basename:var='/abc/def/file.txt'; echo "${var#*.}"; echo "${var##*/}"returnstxtandfile.txt.