简体   繁体   中英

replace second field in a file from git log output

I have a file with full of file routes and timestamps.

xxxx/view/css/animate.css/3.1.1/animate.min.css: 1494509091
xxxx/view/fonts/cinzel/fonts.css: 1494509091
xxxx/view/fonts/garamondcondot-book/fonts.css: 1494509092
xxxx/view/fonts/gotham-narrow/fonts.css: 1494509092
xxxx/view/fonts/gotham/fonts.css: 1494509092
xxxx/view/fonts/greatvibes/fonts.css: 1494509092
xxxx/view/fonts/indie-flower/fonts.css: 1494509092
xxxx/view/fonts/katibeh/fonts.css: 1494509092
xxxx/view/fonts/lobster-two/fonts.css: 1494509092
xxxx/view/fonts/museo/fonts.css: 1494509092
xxxx/view/fonts/myriad-pro/fonts.css: 1494509092

These timestamps isn't right, so i need to check with git log the right last editied timestamp, with this command:

git log -1 --pretty="format:%ct" xxxx/view/css/animate.css/3.1.1/animate.min.css

The problem is that i have more than 500-600 files to check and replace the correct timestamp. I tried with awk, sed but is not good. Here is my examples:

variables=$(git log -1 --pretty="format:%ct") 

     while read line ; do
         replacement="$variables $line"
         sed 's/,.*:/ $replacement/' <<< "$line"
     done < files.yml

another:

  file="files2.yml"
  files=$(cat files.yml | awk '{print $1}' | cut -d ':' -f 1)
  for i in $files;do
    new=$(git log -1 --pretty="format:%ct" $i)
    sed "s/    .*:/ $new/" files2.yml
  done

Have you any tips?

sed -E 's|(.*): .*|echo \x27\1\x27: $(git log -1 --pretty="format:%ct" "\1")|e' files.yml
  • (.*): .* will capture text before last : and space in the line and match rest of the line as well
  • echo \x27\1\x27: to put the file path back, \1 will refer to text matched by (.*) and \x27 is single quote character
  • $(git log -1 --pretty="format:%ct" "\1") the command you want to execute
  • e flag allows to execute the entire modified line as a shell command (not sure if this is specific to GNU sed , but the question is tagged linux so this should be okay)

Note that this is not a robust command. File name with spaces and other shell metacharacters may cause issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM