2017-06-14 14 views
0

ディレクトリツリー内のXMLファイルを検索し、別のディレクトリ(staging_ojs_pootle)にリンクを作成して、ファイルパス(これらのリンクをドットごとに置き換える)に命名する必要があります。xargs変数を置き換えると空の文字列が返される

bashコマンドが機能していないと、私は交換部品につかまってしまいました。私は空の文字列を与え、「ファイル」という名前xargsの変数から、のように思える$ {}結果内の置換コード(${file/\//.})

find directory/ -name '*.xml' | xargs -I 'file' echo "ln" file staging_ojs_pootle/${file/\//.} 

交換内部アクセスできません。/

find directory/ -name '*.xml' | xargs -I 'file' echo "ln" file staging_ojs_pootle/file |sed -e '/^ln/s/\(staging_ojs_pootle.*\)[\/]\(.*\)/\1.\2/g' 

よろしく

+0

を 'file'は、シェル変数の名前ではありませんこれは、処理中のXMLファイルの名前でxargsに置き換えられるプレースホルダーです。したがって、 '$ {file ...}'のようなシェル変数置換コマンドを使用することはできません。いくつかのサンプル入力と期待される出力を見せたら、私たちはあなたを助けることができます。 –

答えて

0

はこのお試しください:たとえば

$ find directory/ -name '*.xml' |sed -r 'h;s|/|.|g;G;s|([^\n]+)\n(.+)|ln \2 staging_ojs_pootle/\1|e' 

$ mkdir -p /tmp/test 
$ touch {1,2,3,4}.xml 
# use /tmp/test as staging_ojs_pootle 
$ find /tmp/test -name '*.xml' |sed -r 'h;s|/|.|g;G;s|([^\n]+)\n(.+)|ln \2 /tmp/test/\1|e' 
$ ls -al /tmp/test 
total 8 
drwxr-xr-x. 2 root root 4096 Jun 15 13:09 . 
drwxrwxrwt. 9 root root 4096 Jun 15 11:45 .. 
-rw-r--r--. 2 root root 0 Jun 15 11:45 1.xml 
-rw-r--r--. 2 root root 0 Jun 15 11:45 2.xml 
-rw-r--r--. 2 root root 0 Jun 15 11:45 3.xml 
-rw-r--r--. 2 root root 0 Jun 15 11:45 4.xml 
-rw-r--r--. 2 root root 0 Jun 15 11:45 .tmp.test.1.xml 
-rw-r--r--. 2 root root 0 Jun 15 11:45 .tmp.test.2.xml 
-rw-r--r--. 2 root root 0 Jun 15 11:45 .tmp.test.3.xml 
-rw-r--r--. 2 root root 0 Jun 15 11:45 .tmp.test.4.xml 
# if don NOT use the e modifier of s command, we can get the final command 
$ find /tmp/test -name '*.xml' |sed -r 'h;s|/|.|g;G;s|([^\n]+)\n(.+)|ln \2 /tmp/test/\1|' 
ln /tmp/test/1.xml /tmp/test/.tmp.test.1.xml 
ln /tmp/test/2.xml /tmp/test/.tmp.test.2.xml 
ln /tmp/test/3.xml /tmp/test/.tmp.test.3.xml 
ln /tmp/test/4.xml /tmp/test/.tmp.test.4.xml 
をsedのが、正規表現は、すべてまたはちょうど最後のスラッシュを交換して使用してみました

E xplains:各XMLファイルの

  1. hold space原点のファイル名を維持するためにhを使用しています。
  2. s|/|.|gを使用して、xmlファイル名を/から.に変更します。
  3. hold spacepattern spaceを追加する場合は、pattern spaceCHANGED_FILENAME\nORIGIN_FILENAMEです。
  4. s|([^\n]+)\n(.+)|ln \2 staging_ojs_pootle/\1|e'を使用してコマンドをCHANGED_FILENAMEORIGIN_FILENAMEにマージし、次にeのsコマンドの修飾子を使用して、上記でアセンブルしたコマンドを実行してください。

希望します。

+0

うまく働いた!どうもありがとうございました –

0

あなたのXMLファイルの名前は任意の単語分割文字が含まれていないことを確認することができます場合は、あなたのようなものを使用することができます

find directory -name "*.xml" | sed 'p;s/\//./' | xargs -n2 echo ln 
関連する問題