简体   繁体   English

在find命令中进行awk替换

[英]Awk substitution within a find command

I am working on a bash script that should find files such as 我正在研究一个应该找到文件的bash脚本

/var/www/templates/testdoctype/test_file.html.head

and return something like 并返回类似的东西

cp -f '/var/www/sites/t/test/test_file.html' '/home/user/tmp/test_file.html'    

my script so far looks like this: 我的脚本到目前为止看起来像这样:

#!/bin/bash
DOCPATH='/var/www/templates/testdoctype'
INSTALL_PATH='/var/www/sites/t/test'
PKGBACKPATH='/home/user/tmp'

function find_suffix_head {
  find "$FROM/$DOCTYPE" -type f -iname "*.head" -print \
  | awk -v docpath="$DOCPATH" -v installpath="$INSTALL_PATH" -v pkgbackpath="$PKGBACKPATH" \
  '{ sub( docpath, installpath ) sub(/.head$/, "") } { printf "cp -f ""'\''"$0"'\''"" " ; sub( installpath, pkgbackpath ) ; print "'\''"$0"'\''" }'
}

find_suffix_head

This returns 这回来了

cp -f '/var/www/templates/testdoctype/test_file.html' '/var/www/templates/testdoctype/test_file.html'

So, sub(/.head$/, "") works as it should, but sub( docpath, installpath ) and sub( installpath, pkgbackpath ) does not. 所以, sub(/.head$/, "")可以正常工作,但是sub( docpath, installpath )sub( installpath, pkgbackpath )却没有。

No need for awk, you can do it with bash: 不需要awk,你可以用bash来做:

function find_suffix_head {
  find "$FROM/$DOCTYPE" -type f -name "*.head" | while read filename; do
    filename=${filename%.head} # strip suffix
    filename=${filename#/var/www/templates/testdoctype} # strip prefix
    echo cp -f "$INSTALL_PATH/$filename" "$PKGBACKPATH/$filename"
  done
}

From there you can just run the cp, too, rather than echoing it. 从那里你也可以运行cp,而不是回应它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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