regex - How to rename multiple directories into corresponding file names in bash -


i need bash script rename directory:

i want rename 6 directories first in order conduct analysis:

this original directory name rename:

mcia-1-control_20170509 mcia-2-control_20170509 mcia-03-timesport_20170717 mcia-04-timesport_20170717 mcia-05-timesport_20170717 mcia-6-timesport_20170717 

i need rename following:

subj-1-control subj-2-control subj-3-timesport subj-4-timesport subj-5-timesport subj-6-timesport 

i declared several variables below:

$fnids #e.g.mcia-1-control_20170509...  $ren #e.g. 1-control... $raw_dir #data directory 

then run loop, results did not expect:

for fn in ${fnids};      echo "+++this folder name rename: fnids:${fnids}+++"     echo "+++renameing folder: fn:${fn}+++"      rn in ${ren};         echo "=======add "subj" rename subj folder==========="         refn=subj-"$rn" #e.g. subj-1-control...         echo "refn:$refn"         echo "rename folder $fn $refn"          if ( test -d "$raw_dir"/"$fn"); #if dir exist             cp -r "$raw_dir"/"$fn" "$raw_dir"/"$refn" #copy , rename file             echo "cp rename subj folder:$raw_dir/$refn"         elif ( test -d "$raw_dir"/"$refn");then             echo "++++++renamed subject folder exists+++++++"                    else             echo "+++++somthing went wrong!name of subject folder not exists check "$fn $refn"+++++++++"             exit         fi     done done 

when execute above script, doing things below:

+++renameing folder: fn:mcia-1-control_20170509+++ =======rename subj folder=========== renfn:subj-1-control rename folder mcia-1-control_20170509 subj-1-control rename folder mcia-1-control_20170509 subj-2-control 

what want is:

rename folder mcia-1-control_20170509 subj-1-control rename folder mcia-2-control_20170509 subj-2-control... 

basically, rename original folder name 6 different subj folder name... got stuck here while...

thanks me out...

this can done using bash regular expression matching capturing groups.

code

read -r -d '' input <<end mcia-0-control_20170509 mcia-1-control_20170509 mcia-2-control_20170509 mcia-03-timesport_20170717 mcia-04-timesport_20170717 mcia-05-timesport_20170717 mcia-6-timesport_20170717 end  name in $input;     [[ $name =~ ^mcia-0*([[:digit:]]+)-(control|timesport)_[[:digit:]]+$ ]] &&             echo mv "$name" "subj-${bash_rematch[1]}-${bash_rematch[2]}" done 

output

mv mcia-0-control_20170509 subj-0-control mv mcia-1-control_20170509 subj-1-control mv mcia-2-control_20170509 subj-2-control mv mcia-03-timesport_20170717 subj-3-timesport mv mcia-04-timesport_20170717 subj-4-timesport mv mcia-05-timesport_20170717 subj-5-timesport mv mcia-6-timesport_20170717 subj-6-timesport 

explanation

  • ^: match start of string.
  • mcia-: match current directory prefix.
  • 0*: optionally match number of leading zeroes. these not go capturing group, because want discard them.
  • ([[:digit:]]+): after discarding leading zeroes, capture 1 or more remaining digits first capture group.
  • -: match , discard additional hyphen.
  • (control|timesport): capture either "control" or "timesport" second capture group.
  • _[[:digit:]]+: match rest.
  • $: match end of string.

if input string match, build mv command destination concatenation of "subj", first capture group , second capture group. testing purposes, code sample uses echo print command instead of running it. can expand necessary validation, existence checks, etc. doing in original code sample.

for more details on regular expressions in bash, see advanced bash-scripting guide - 18.1. brief introduction regular expression.


Comments

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -