shell贪婪匹配

shell从入门到精通(23)贪婪匹配、非贪婪以及独占模式 非贪婪匹配会尽可能少地匹配符合模式的字符,只匹配满足条件的最少字符数。例如,在正则表达式\d+中,加号表示匹配一个或多个数字,而贪婪匹配会尽可能多地匹配连续的数字,直到遇到非数字字符为止。通过使用问号来实现非贪婪匹配,我们可以得到我们需要的最小匹配结果。第一个示例中,使用贪婪匹配模式\d+匹配了字符串中的尽可能多的数字,并将结果打印出来。简而言之,贪婪匹配会尽可能多地匹配,而非贪婪匹配会尽可能少地匹配,根据具体的需求选择适当的匹配方式。在正则表达式中,贪婪匹配和非贪婪匹配是指匹配模式下的不同行为。 阅读详情

有时候对一段文字只提取其中的一部分,可以借助shell的贪婪匹配实现,%是从右向左匹配,#则是从左向右匹配,如下实例:

[root@www ~]# qq=www.qq.com

[root@www ~]# echo ${qq#*.}      //排除第一个
qq.com
[root@www ~]# echo ${qq##*.}    //排除前两个
com
[root@www ~]# echo ${qq%.*}     //排除后一个
www.qq
[root@www ~]# echo ${qq%%.*}  //排除后两个
www
[root@www ~]#


以上是一个简单的实例,下面来看一个具体的脚本中的使用

# vim test.sh
# chmod +x test.sh
# cat test.sh

#!/bin/bash

num=1
for i in *.tar *.tar.gz
do
new=new_$num.${i#*.}    
mv $i $new 2>/dev/null

if [ $? -eq 0 ];then
    echo "remove $i to $new"
    let num++
fi
    
done
# touch test{1..5}.tar test{1..5}.tar.gz
# ls
test1.tar     test2.tar     test3.tar     test4.tar     test5.tar     test.sh
test1.tar.gz  test2.tar.gz  test3.tar.gz  test4.tar.gz  test5.tar.gz
# ./test.sh
remove test1.tar to new_1.tar
remove test2.tar to new_2.tar
remove test3.tar to new_3.tar
remove test4.tar to new_4.tar
remove test5.tar to new_5.tar
remove test1.tar.gz to new_6.tar.gz
remove test2.tar.gz to new_7.tar.gz
remove test3.tar.gz to new_8.tar.gz
remove test4.tar.gz to new_9.tar.gz
remove test5.tar.gz to new_10.tar.gz
#




ComfyUI安装包运行前准备:安装Git Git 是一个分布式版本控制系统(Version Control System, VCS),用于跟踪和管理文件(尤其是代码)的变更历史。 简单理解就是,git是用来管理文件的版本的,如果有新的版本,就可以使用git来更新。 阅读详情

相关推荐

Calibre电子书阅读器个性化样式定制指南

本文详细介绍了如何通过Calibre电子书阅读器的用户样式表功能进行深度个性化样式定制。从修改全局字体、行距、背景配色(包括深色/浅色模式)到美化代码块、引用等特殊元素,本指南提供了完整的CSS代码示例与实战技巧,帮助用户打造专属的舒适阅读环境,显著提升电子书阅读体验。

vv45678的博客 920

shell脚本数组及正则表达式

1. 数组名=(value0 value1 value2 value3 ......)2. 数组名=([0]=value [1]=value1 [2]=value2 ....)3. 列表名="value0 value1 value2 value3 ...... "数组名=($列表名)4. 数组名[0]="value1"数组名[1]="value2"数组名[2]="value3"

weixin_53585291的博客 468

别再写前端了!用Gradio 3.x + HuggingFace Spaces,5分钟把你的Python算法变成网页应用

本文介绍如何利用Gradio 3.x和HuggingFace Spaces快速将Python算法转化为网页应用,无需前端开发经验。通过5行代码和简单的Git命令,即可实现专业级Web界面部署,大幅提升算法展示效率。特别适合算法工程师和数据科学家快速分享模型成果。

weixin_33443333的博客 332

shell 分割字符串_谈一谈Shell中的贪婪匹配和非贪婪匹配

Shell中的贪婪匹配和非贪婪匹配非贪婪匹配,即匹配符合通配符的最短结果。贪婪匹配,即匹配符合通配符的最长结果。下面案例为例:1、${VALUE%.*}或${VALUE%%.*}:删除VALUE字符串中以分隔符"."匹配的右边字符,保留左边字符。简单解释:*:通配符,用于匹配字符串将被删除的子串。.:分割符,可以为任意一个或多个字符。%:从右向左匹配。%:非贪婪匹配,即匹配符合通配符的最短结果。%...

weixin_39716043的博客 450

SAP WM模块配置详解

1.给工厂/存储地点分配仓库编码 功能说明 配置路径 IMG>企业结构>分配>分配后勤执行>给工厂/存储地点分配仓库编码 业务示例 配置步骤 2.定义仓库号码控制参数 功能说明 配置路径 IMG>后勤执行>仓库管理>主数据>主数据>定义仓库号码控制参数 业务示例 配置步骤 3.定义编码范围 功能说明 配置路径 IMG>后勤执行>仓库...

weixin_40672823的博客 2万+

目标检测技术的发展:从R-CNN、YOLO到DETR、DINO

OV-DINO的出现标志着在开放域目标检测领域的重大进步,其在处理未见过的物体类别时的能力得到了显著提升。这项技术对于需要适应未知或变化环境的应用具有重要意义,如自动驾驶、机器人视觉、安防监控等。

DeepAIedu的博客 2629

shell 中 贪婪匹配 和 非贪婪匹配

举个栗子:    1 2 v=jfedu.shiyiwen.com echo ${v%.*}  一个%为非贪婪匹配,即匹配最短结果。%从右到左进行非贪婪匹配,匹配什么呢? 匹配已 .*的字符。 那么当然是匹配到了.com 然后执行删除匹配到的字符。结果就是(这里的 "." 相当于定界符。而“*”是通

木子儿 7731

【AI编程】Trae 环境搭建与配置从使用到项目实战详解

Trae 环境搭建与配置从使用到项目实战详解

congge 5650

shell sed贪婪匹配

shell中sed 利用正则表达式匹配字符串时,默认使用贪婪模式,且只有贪婪模式,没有非贪婪模式。 贪婪模式:匹配最长的字符串,开始尽量早、结束尽量晚 http://blog.chinaunix.net/uid-10540984-id-2954393.html ...

weixin_44660094的博客 2475

[QMT量化交易小白入门]-九、如何用Python记录每一笔交易和调试,报错信息?

定义一个函数来获取日志记录器。: 返回全局的日志记录器,以便在其他地方调用。

木头大左的博客 2143

qq对接openclaw,通过qq发送命令

把 OpenClaw 接入 QQ 和微信,目前主流的微信文章和教程主要提供了两种截然不同的路径。我为你梳理了它们的核心区别和关键步骤,你可以根据自己的需求来选择。

suny8的专栏 270

shell脚本获取指定目录下指定格式文件数量和名称以及贪婪匹配和非贪婪匹配

shell脚本获取指定目录下指定格式文件数量和名称 #!/bin/bash #创建一个集合存放遍历出来的数据 zip_list=() controller_tar(){ for file in `ls 指定文件夹的绝对路径` do #贪婪匹配文件后缀名是否为zip或者gz if [ "${file##*.}"x = "zip"x ]||[ "${file##*.}"x = "gz"x ] then #如果符合条件将文件放入集合中 zip_list[${#zip

qq_41640103的博客 1632

2026 AI浏览器Agent终极对比:BrowserAct、agentBrowser、PP-Browser 谁更适合落地?(安装+实战+场景全覆盖)

1.不要盲目追求最新框架:很多人跟风用agentBrowser,结果业务是多步骤复杂办公流程,反而不如BrowserAct稳定;2.风控场景优先PP-Browser:但凡需要登录账号、频繁访问正规平台,无脑选PP-Browser,能避开99%的封禁问题;3.AI批量解析优先agentBrowser:做知识库爬取、网页内容总结、批量问答,省Token就是省成本,长期使用性价比碾压另外两者;4.个人开发调试优先BrowserAct:容错高、日志全、调试简单,新手上手最快,不容易报错翻车。

十年老程序猿,AI 疯狂研究专家,前沿硬核知识分享 327

linux shell grep 非贪婪匹配

默认情况下匹配都是贪婪模式,如果要改成非贪婪模式,只需要量词后面加上一个问号? 贪婪模式常用的量词有: {m,n} {m,} ? * + 如果改成非贪婪模式,只需这样: {m,n}? {m,}? ?? *? +? Linux上grep执行不生效,和bsd和GUN协议有关,bsd用grep -oE GUN用grep -oP l例子: centos上执行 ech...

qq_35702095的博客 7454

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

shell sed非贪婪模式字符区间匹配

这里写自定义目录标题欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入 sed 费贪模式匹配: 字符串 fsd=fsd1,anchor=test,depth=4,width=4,files=500

u010094569的博客 1670

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

logstash判断是否匹配_logstash 中的贪婪匹配

logstash 中的贪婪匹配:10.252.142.174 - - [06/Sep/2016:08:41:36 +0800] "GET /api/validate/code/send?mobilePhone=18652221499&messageType=1&_=1454297673274 HTTP/1.1" 200 52 0.010 112.17.240.27表达式:%{IP...

weixin_30895059的博客 400

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

Shell脚本编程之正则表达式

在 Shell 脚本中,正则表达式是一种强大且常用的文本处理工具,它可以用来匹配、搜索、替换和截取字符串。 正则表达式是由一些字符去描述规则,在正则表达式中有两类字符 (1)元字符(Meta Character):Shell 环境中具有特殊含义的字符,在命令行解释、文件名扩展、变量替换等方面起着关键作用。 (2)普通字符:仅代表自身的字符。\元字符 ==> 普通字符 正则表达式也有不同的流派(如Egrep,java,C#)很多语言都用到了正则表达式,但是这些语言中用来描述字符串规则的"元字符"不是都一样的,

weixin_49337111的博客 1453

linux获取字符串最后几位,Shell截取字符串的8种方法

Linux 的字符串截取很有用。有八种方法。假设有变量 var=http://www.aaa.com/123.htm.1. # 号截取,删除左边字符,保留右边字符。 (非贪婪匹配)echo ${var#*/}其中 var 是变量名,# 号是运算符,*/ 表示从左边开始删除第一个 / 号及左边的所有字符即删除 http://结果是 :www.aaa.com/123.htm2. ## 号截取,删除左边...

weixin_42524864的博客 2万+

[2019.03.22] Linux 学习心得(1)

本文关键词:shell 判断、grep正则表达式使用和贪婪匹配理解 1. if [ $a -le $b ], 一开始自学的时候我以为 [ ... ] 就是普通的,语法规定的结构,结果其实人家是"test"的变体,它的效果等同于使用 " test ... ",结果是将指令 ... 执行完了以后输出出来的东西作为返回值。注意,他并不是能执行所有的命令,作为“判断符号”,他要求里面的指令运行结果...

weixin_30378623的博客 82

shell 字符串截取表达式

${var#str} 从左向右匹配,非贪婪匹配,截取并保留右边的内容 txt='123456abc123456' echo ${txt#*34} # 56abc123456 ${var##str} 从左向右匹配,贪婪匹配,截取并保留右边的内容 txt='123456abc123456' echo ${txt##*34} # 56 ${var%str} 从右向左匹配,非贪婪匹配,截取并保留左边的内容...

weixin_30527143的博客 119

shell中变量内容的删除

# 从前向后匹配 ## 从前向后匹配,贪婪匹配 % 从后向前匹配 %% 从后向前匹配,贪婪匹配

嵌入式 551

Linux Shell 用法(二)split和VAR命令

split命令可以用来分割文件 $ split -b 10k data.file $ ls data.file xaa xab xac xad xae xaf xag xah xai xaj 上面的命令将data.file分割成了10个大小为10KB的文件。 如果想使用数字后缀,需要使用-d选项。此外, -a length 可以指定后缀长度: $ split -b 10k data.file -...

qq_33544833的博客 651

Shell 通过#和%截取变量

使用变量操作符 ${} 1、单个#符:截掉左侧,非贪婪匹配 ${var#*.} 该方式是从左开始第一次匹配到字符".",然后截掉左边内容(包括字符"."),返回余下右侧部分。 $ var=/dir1/dir2/file.tar.gz $ echo ${var#*.} tar.gz 2、双#符:截掉左侧,贪婪匹配 ${var##*/} 该方式是从左开始最大化匹配到字符"/...

boweiqiang的博客 570

sed替换字符串的贪婪匹配和最小匹配

最近在shell对文本进行操作,遇到了使用sed进行最小匹配的问题。sed默认匹配规则是贪婪匹配。 举例来说,如果文本内容为: (aa)bb(cc) 使用sed匹配: '/(.*)/' 匹配结果为: (aa)bb(cc) 上述结果即贪婪匹配。 最小匹配的结果是获取规则字符串之间的最小内容,即为: (aa) (cc) 这两个结果。 sed获取最小匹配的方法: 1.通过...

u013769481的博客 1万+

字符串从右截取_Shell处理字符串常用技巧之分割、替换字符串

Shell处理字符串常用技巧之分割、替换字符串在Shell编程中,经常要处理一些字符串变量。下面给大家整理Shell处理字符串常用分割、替换字符串。1、 ${#VALUE}:计算VALUE字符串的字符数量。2、 ${VALUE%.*}或${VALUE%%.*}:删除VALUE字符串中以分隔符"."匹配的右边字符,保留左边字符。3、 ${VALUE#*.}或${VALUE##*.}:删除VALUE字...

weixin_30269739的博客 748
上一篇: initrd解压和相关制作
下一篇: RHEL6 双网卡绑定
kumu_Linux
博客等级 码龄14年 68粉丝 94原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值