Homemade for personal use - record it
1. Demonstration
Self-made and self-use - use commands in the terminal, find keywords, and print the outline of notes
Two, function
Main functions of the script:
- According to the keyword, output all branches
Extended function:
- Set the number of output lines as needed
- search keyword highlighting
- exact match/fuzzy match
3. Process
I started to study recently, and I wrote a lot of mind maps while watching online classes, but in actual use, it is not very convenient to open the mind maps frequently, so I want to use
commands like –help to view my own mind maps in the terminal picture
The first is to be able to read the mind map, and later found that the curtain can convert the mind map into an outline note mode:
Then export it as docx, and then convert it into txt format in batches (because you need to install other things to read files in docx format)
libreoffice --headless --convert-to txt /home/qwe/下载/*.docx --outdir /home/qwe/桌面/script/lib
After it can be read, it is to find a way to output the branch of the node where the keyword is located
The method adopted is: get the number of spaces at the beginning of each line, the larger the number of spaces, it means that it is a child node, and it can be output
#!/bin/bash
# 接收参数:参数1:待查询关键词 参数2:最大输出行数
key=${1:-' '}
maxlines=${2:-100}
if [[ $key == ' ' ]];then
echo "参数1:查询关键词(必填)"
echo "参数2:最大输出行数(默认为100):从查找到关键词的行,继续往下输出的行数(需满足是分支)"
exit
fi
# echo "查询关键词:${key}"
# echo "最大输出行数:${maxlines}" #从查找到关键词的行,继续往下输出的行数(需满足是分支)
# 若无下行代码,行内文本的缩进和空格将会丢失。
IFS=$'\n'
flag=0 # 为1,开始输出分支
start_count=0 # 起始行 空格个数
cur_count=0 # 空格个数
src=0 #未输出过文件名
dir="/home/qwe/桌面/script/lib/"
ls $dir | while read srcfile # 文件
do
let src=0
for line in `cat ${
dir}${
srcfile}` # 每行
do
if [ $flag -gt 0 ]
then # 若是分支,返回包含关键字的分支
# 判断此行空格是否大于start_count
let cur_count=0 # 空格个数
for i in `seq 1 ${
#line}` # 每个字符
do
a=`echo $line | cut -c $i`
case $a in
' ')
let cur_count+=1
;;
# ● )
*)
break
;;
esac
done
# echo "cur_count:${cur_count} --$line"
# 如果没有空格,就输出!待更新
if [ $cur_count -eq 0 ]
then
echo $line | perl -pe "s/${key}/\e[1;31m$&\e[0m/g"
elif [ $cur_count -gt $start_count ]
then
echo $line | perl -pe "s/${key}/\e[1;31m$&\e[0m/g"
let cur_count=0
let flag-=1
if [ $flag -eq 0 ];then
let start_count=0 # 起始行 空格个数
let cur_count=0 # 空格个数
echo "---------------------------------------------------------"
fi
else
let flag=0 # 为1,开始输出分支
let start_count=0 # 起始行 空格个数
let cur_count=0 # 空格个数
echo "---------------------------------------------------------"
# 本以为是分支,但其实不是,又没法进入elif判断,所以这里要添加判断
if [[ $line =~ $key ]] # 包含关键字
then # 返回包含关键字的分支
let flag=${maxlines}
echo $line | perl -pe "s/${key}/\e[1;31m$&\e[0m/g"
for i in `seq 1 ${
#line}`
do
a=`echo $line | cut -c $i`
case $a in
' ')
let start_count+=1
;;
*)
break
;;
esac
done
# echo "start_count:$start_count"
fi
fi
elif [[ $line =~ $key ]] # 包含关键字
then # 返回包含关键字的分支
# 可能不是要找的,比如cpp
let flag=${maxlines}
if [[ $src == 0 ]]
then
let src=1
echo " "
echo "======================= "$srcfile" ======================="
fi
echo $line | perl -pe "s/${key}/\e[1;31m$&\e[0m/g"
let start_count=0 # 起始行 空格个数
for i in `seq 1 ${
#line}`
do
a=`echo $line | cut -c $i`
case $a in
' ')
let start_count+=1
;;
*)
break
;;
esac
done
# echo "start_count:$start_count"
fi
done
done
After the code is written, you need to set an alias for it, otherwise it will be too troublesome to use every time
gedit ~/.zshrc
qwe /home/qwe/桌面/script/qwe.sh
source ~/.zshrc
Then you can use the command
qwe #查看命令
qwe rm # 参数1:查询关键词(必填)
qwe rm 3 #参数2:最大输出行数(默认为100):从查找到关键词的行,继续往下输出的行数(需满足是分支)
4. Harvest
1. Shell pass parameters, variables , parentheses
${var-初始值} 如果没有定义,则表达式返回默认值
没有定义或者为空字符串,则表达式返回默认值
${var:-初始值} 临时调用,变量var并不改变,仍然为空
${var:=初始值} 调用初始值后,变量var也赋予了一个值
传=${1:-'1.0.0'}
参=${2:-'test demo'}
Both $( ) and ` ` (backtick) can be used for command substitution
Use ∗ ∗ variable substitution ∗ ∗ . In general, { } is used for **variable substitution**. In general,for∗∗ Variable substitution∗∗ . In general, var is no different from ${var}
[ ] sum [] sum[ ] and (()) are the same, bothperform mathematical operations.
(( )) and [[ ]] are enhanced versions of [ ] for mathematical comparison expressions and string expressions , respectively.
2.case in
3.if else
4. Highlight
echo \$line | perl -pe "s/\${key}/\e[1;31m$&\e[0m/g"
6.IFS
8. Get the number of spaces in each line
let start_count=0 # 起始行 空格个数
for i in `seq 1 ${
#line}`
do
a=`echo $line | cut -c $i`
case $a in
' ')
let start_count+=1
;;
*)
break
;;
esac
done
# echo "start_count:$start_count"
9.doc to txt
libreoffice --headless --convert-to txt /home/qwe/下载/*.docx --outdir /home/qwe/桌面/script/lib
Five, the problem
Keyword matching , no exact match ,
fuzzy match