1、shell取文件第一行特定字符,并存入到某个文件中
背景说明:
1、文件第一行内容 summary: Prec@1: 73.408, Prec@5: 91.338
2、期待提取数值:73.408和91.338
脚本:
#! /bin/bash
for filename in \
pytorch_densenet121.txt \
pytorch_densenet161.txt
do
fistline=$(head -n +1 ${filename})
section=${fistline#*Prec@1:}
top1=${section%, Prec@*}
top5=${section#*Prec@5:}
echo ${filename} >> 1.txt
echo ${top1} >>1.txt
echo ${top5} >>1.txt
done
1、shell取文件最后一行的特定字符,并存入到某个文件中
背景说明:
1、最后一行内容为:n02437616/00050000.yuv Class:355 Predicted: Category:355 Score:0.49561 Category:354 Score:0.41089 Category:9 Score:0.03372 Category:348 Score:0.01595 Category:388 Score:0.00777Prec Top1:72.25000Prec Top5:90.85200
2、期待提取top1和top5数值,即72.25000、90.85200
参考:Shell字符串截取的实现方法(非常详细)_linux shell_脚本之家 (jb51.net)
脚本:
#! /bin/bash
for filename in \
a.txt \
b.txt \
c.txt
do
lastline=$(cat ${filename} | awk 'END {print}')
section=${lastline#*Top1:}
# echo ${section}
top1=${section%Prec*}
top5=${section#*Top5:}
echo ${filename} >> 1.txt
echo ${top1} >>1.txt
echo ${top5} >>1.txt
done
结果文件:
2、shell修改文件名称,将某段字符串开始之后的部分去除
参考:linux 批量修改文件名 文件名只保留部分,去掉部分 - 小麦333 - 博客园 (cnblogs.com)
#! /bin/bash
for file in \
mxnet_darknet_kl_divergenceresnet50_top5_values_2022-02-25-10-33-37.txt.txt \
mxnet_DenseNet_kl_divergenceresnet50_top5_values_2022-02-25-10-31-18.txt.txt
mv ${file} ${file%%resnet50_top5*}.txt
done
3、批量替换文件中的某个字符串
#! /bin/bash
for file in \
mxnet_SqueezeNet1.1_int8_percentile.json \
mxnet_SqueezeNet1.0_int8_percentile.json
do
sed -i "s|hw_config.json|hw_config_batchsize4.json|" ${file}
done
4、shell循环顺序执行命令,而不是并行执行
#! /bin/bash
odel_map=(
"onnx_mnasnet0"
"onnx_gluon"
"onnx_densenet201"
)
for i in ${model_map[@]}
do
echo $i
python3 run.py --dataset_path "/home/img/" --model_info_file "./$i.json" --vdsp_params_info "./vdsp.json" --result_path "./result/${i/_int8/_int8_yuv_}"
done
5、后台执行某个脚本
nohup ./1.sh >a.log &
6、vi中字符替换
将所有aaa替换为bbb:%s/aaa/bbb/g
将某行所有aaa替换为bbb::10s/aaa/bbb/g
将某行第一个aaa替换为bbb:10s/aaa/bbb/
注释某行:10s/^/#/g
二、notepad
1、行首^
2、行结尾$