I have a file abc.123.234.345
and I want to store value of 123.234.345
into variable1 using UNIX oneliner. Then I have to rename a file named def
as def.123.234.345
I tried sed
, awk
but not able to generate split string.
我有一个文件abc.123.234.345,我想使用UNIX oneliner将值123.234.345存储到variable1中。然后我必须将名为def的文件重命名为def.123.234.345我试过sed,awk但是不能生成拆分字符串。
I have to search as abc.*.*.*
as string will be different every time.
我必须搜索为abc。*。*。*,因为字符串每次都会有所不同。
Any suggestions?
有什么建议么?
Some sample that I use but not working
我使用的一些样品但不起作用
FILE=abc.*.*.* | mv /loc/def /loc/def.${FILE#*.}
1 个解决方案
#1
2
Answer for revised question
For the question as revised in the most recent comment for which there is only one file matching the glob abc.*.*.*
and that file is in the current directory:
对于最新评论中修改的问题,其中只有一个文件与glob abc。*。*。*匹配,并且该文件位于当前目录中:
find . -maxdepth 1 -name 'abc.*.*.*' -exec bash -c 'mv /loc/def "/loc/def.${1#./*.}"' none {} \;
Or, in two steps:
或者,分两步:
file=$(echo abc.*.*.*)
mv /loc/def "/loc/def.${file#./*.}"
Note that it is best practice to use names for shell variables that are lower or mixed case. The system uses upper case names for its variables and you don't want to accidentally overwrite one of them.
请注意,最佳做法是使用较低或混合大小写的shell变量的名称。系统为其变量使用大写名称,您不希望意外覆盖其中一个变量。
Answer for original question
This does what you ask:
这就是你所要求的:
find . -name 'abc.*.*.*' -exec bash -c 'mv /loc/def "/loc/def.${1#./*.}"' none {} \;
The above execute mv
commands such as:
以上执行mv命令如:
mv /loc/def /loc/def.123.234.345
Note that, after the first mv
command, /loc/def
will no longer exist and the second mv
command will therefore fail.
请注意,在第一个mv命令之后,/ loc / def将不再存在,因此第二个mv命令将失败。
#1
2
Answer for revised question
For the question as revised in the most recent comment for which there is only one file matching the glob abc.*.*.*
and that file is in the current directory:
对于最新评论中修改的问题,其中只有一个文件与glob abc。*。*。*匹配,并且该文件位于当前目录中:
find . -maxdepth 1 -name 'abc.*.*.*' -exec bash -c 'mv /loc/def "/loc/def.${1#./*.}"' none {} \;
Or, in two steps:
或者,分两步:
file=$(echo abc.*.*.*)
mv /loc/def "/loc/def.${file#./*.}"
Note that it is best practice to use names for shell variables that are lower or mixed case. The system uses upper case names for its variables and you don't want to accidentally overwrite one of them.
请注意,最佳做法是使用较低或混合大小写的shell变量的名称。系统为其变量使用大写名称,您不希望意外覆盖其中一个变量。
Answer for original question
This does what you ask:
这就是你所要求的:
find . -name 'abc.*.*.*' -exec bash -c 'mv /loc/def "/loc/def.${1#./*.}"' none {} \;
The above execute mv
commands such as:
以上执行mv命令如:
mv /loc/def /loc/def.123.234.345
Note that, after the first mv
command, /loc/def
will no longer exist and the second mv
command will therefore fail.
请注意,在第一个mv命令之后,/ loc / def将不再存在,因此第二个mv命令将失败。