1

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.

bstabens
  • 59
  • 7
  • 1
    use /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:30
  • Oh, sorry, I lost that bracket in copying. I checked this in my script multiple times. Also sorry for the wrong shebang, am working via ssh and simply got confused as to which I was using (not that it works in bash, either...) – bstabens Aug 25 '16 at 14:02
  • do you get syntax error if you remove the if..fi portion of the script? if not, you will have to post the code in if..fi as well – Sundeep Aug 25 '16 at 14:20
  • No, same thing without the if. – bstabens Aug 26 '16 at 04:13
  • well, I don't get any syntax error.. you can try out http://www.shellcheck.net/# – Sundeep Aug 26 '16 at 04:16
  • Shellcheck likes my script and only recommends to double quote the echoed variables... but my Bash-Script at my Raspberry Pi still doesn't run. (And yes, I already found someone doing this successfully on the Pi!) – bstabens Aug 26 '16 at 06:07
  • It works fine for me.  Trim your code down to the bare essentials (removing anything that you don’t want to share with us), and, if it still exhibits the problem, then post the entire script. Also, (1) Indent properly.  Consider breaking long commands into multiple lines (using \, if necessary).  (2) Change for i in `ls`⁠ to for i in *.  (3) Change \3) to \3. – G-Man Says 'Reinstate Monica' Aug 26 '16 at 08:06
  • You should always quote your shell variable references (e.g., "$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

1 Answers1

0

If it were me, I would load up the array like this:

#!/bin/bash

string="file_20160825_namegoeson"

myarray[0]="${string:5:4}"
myarray[1]="${string:9:2}"
myarray[2]="${string:11:2}"

echo "${myarray[0]}"
echo "${myarray[1]}"
echo "${myarray[2]}"
bashBedlam
  • 1,049