Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何使用find(或其他命令)提取文件名中下划线后的数字_Bash_Find_Counter - Fatal编程技术网

Bash 如何使用find(或其他命令)提取文件名中下划线后的数字

Bash 如何使用find(或其他命令)提取文件名中下划线后的数字,bash,find,counter,Bash,Find,Counter,这应该是一个快速的人谁可以编码,不像我 我正在尝试编写一个基本的bash脚本,它将: (i) 查看目录中名为counter_u的单个文件的编号,例如counter_1、counter_2,然后在_ (ii)根据提取的数字创建新变量,以控制任务 (iii)以增量1将新计数器保存到同一位置 (iv)重复 i、 e.如果计数器_1是位置中的文件: find /path/to/file -name 'counter_*' | some magic that extracts the digits aft

这应该是一个快速的人谁可以编码,不像我

我正在尝试编写一个基本的bash脚本,它将:

(i) 查看目录中名为counter_u的单个文件的编号,例如counter_1、counter_2,然后在_ (ii)根据提取的数字创建新变量,以控制任务 (iii)以增量1将新计数器保存到同一位置 (iv)重复

i、 e.如果计数器_1是位置中的文件:

find /path/to/file -name 'counter_*' | some magic that extracts the digits after the _|
CounterNumber="number extracted above"

# my script here using the CounterNumber variable

rm  /path/to/file/counter_$CounterNumber
touch /path/to/file/counter_$(CounterNumber+1)
以此类推,对计数器2重复此过程

提前谢谢你的帮助

CounterNumber="$(find /path/to/file -name 'counter_*' | awk -F_ 'BEGIN{sum=0} {sum+=$NF} END{print sum}')"
# your script...
rm "/path/to/file/counter_$CounterNumber"
touch "/path/to/file/counter_$((CounterNumber+1))"
让我们把第一行的
awk
调用分解

awk -F_ 'BEGIN{sum=0} {sum+=$NF} END{print sum}'
发件人:

-F fs

--字段分隔符fs

Use fs for the input field separator (the value of the FS predefined variable).
这意味着
awk
将拆分
\u
上的文本,如果未指定
-F
,默认情况下将使用空格

在执行任何处理之前,
awk
表达式的
BEGIN
部分将运行一次。在这里,我们初始化一个变量,它将跟踪我们的和

第二个
{}
组将为馈送到
awk
的每一行运行,用最后一个字段更新变量
sum
。在本例中,
$1
=
计数器
$2
(以及最后一个字段,
$NF
)是数字


表达式的
END
部分将在退出
awk
之前运行一次,因此只要打印出
sum

的值,假设您的
find
命令按需工作,并且文件名始终为
counter_
CounterNumber=$(find…| cut-d“-f2)
;要增加变量:
CounterNumber=$((CounterNumber+1))
谢谢!计数器编号=$(find/path/file-name'counter_*'| cut-d“*-f2)计数器编号=$((计数器编号+1))