linux - 重击。读取符号之间的输入“单词”字符串
问题描述
我在创建的 bash 中有这种输入:
猫狗狮子
我需要我的程序对每个单词做一些这样的事情
`if first_word == "cat"
do stuff
if second_word == "dog"
do other stuff`
我该怎么做?谢谢
解决方案
BASH 解决方案:
input='cat-dog-lion'
if [[ "${input}" =~ "^cat-?" ]]
then
echo "do stuff"
if [[ "${input}" =~ "^cat-dog-?" ]]
then
echo "do other stuff"
fi
fi
如果想在这里使用 AWK 解决方案:
awk -F'-' '{ if ($1=="cat") { print("do stuff"); if ($2=="dog") print("do other stuff"); } }'
测试:
$ echo 'cat-dog-lion' | awk -F'-' '{ if ($1=="cat") { print("do stuff"); if ($2=="dog") print("do other stuff"); } }'
do stuff
do other stuff
$ echo 'cat-wolf-lion' | awk -F'-' '{ if ($1=="cat") { print("do stuff"); if ($2=="dog") print("do other stuff"); } }'
do stuff
$ echo 'lynx-dog-lion' | awk -F'-' '{ if ($1=="cat") { print("do stuff"); if ($2=="dog") print("do other stuff"); } }'
$
推荐阅读
- go - 无法访问 Go 模板中范围内的模板值或结构值
- reason - 一般如何将变体类型的值转换为字符串?
- r - 根据条件 (R) 在数据框中制作中位数和四分位数的数据拆分列
- html - 有没有办法使用 css 更改网站的宽度?- HTML
- angular - 如何清除 Angular 中的 div 内容?(角度 7)
- sql - 获取满足所有条件的记录
- javascript - skybox boxgeometry 没有出现。只显示黑屏
- autodesk-forge - 有关在 Autodesk Forge Reality Capture 中使用测量文件、地面控制点和检查点的问题
- javascript - 谷歌云数据存储,javascript,无法使用 query.order
- forms - Flutter 中的多步骤表单(向导样式) - 最佳方法