I'm trying to rename multiple files containing dates. I want to split the date into year, month and day and then create a new filename in a directory tree like year/month-year/filename_yearmonthday.extension
I already succeeded in creating a sed expression to filter out the date and write it back into three separate variables. I would now like to insert the values into an array, where [0] is year, [1] is month and [2] is day. I tried the following statement:
#!/bin/bash
for i in *
do
myarray=( $(echo ${i} | \
sed -n 's/.*\([0-9]\{4\}\)\([0-9]\{2\}\)\([0-9]\{2\}\).*/\1 \2 \3/p' ) )
done
but the script complains about Syntax error: "(" unexpected (expecting "done") in the third line containing the sed-expression.
***edit The position of the date keeps changing, so I can't just split by strings.
As of now, I don't even succeed in something like this:
#!/bin/bash
myarray=(1 2 3 4)
echo ${myarray[@]}
it always tells me "Syntax error: "(" unexpected (expecting "done")" in the line containing the array. ***endedit
If I do this directly in the command line, it's working. Thanks for the help.
/bin/bash
if you want a bash script.. regarding syntax error, I think\3)/p' )
should be\3/p' ) )
.. there are other issues in your script as well, such as http://mywiki.wooledge.org/ParsingLs – Sundeep Aug 25 '16 at 11:30if..fi
portion of the script? if not, you will have to post the code inif..fi
as well – Sundeep Aug 25 '16 at 14:20\
, if necessary). (2) Changefor i in `ls`
tofor i in *
. (3) Change\3)
to\3
. – G-Man Says 'Reinstate Monica' Aug 26 '16 at 08:06"$i"
and"${myarray[@]}"
) unless you have a good reason not to, and you’re sure you know what you’re doing. By contrast, while braces can be important, they’re not as important as quotes, so there’s no benefit to saying${i}
or"${i}"
in your context. See quoting – using single or double bracket in bash and Security implications of forgetting to quote a variable in bash/POSIX shells. – G-Man Says 'Reinstate Monica' Aug 27 '16 at 09:46