textFileA包括:

mango:oval:yellow:18pcs
apple:irregular:red:12pcs
orange:round:orange:4pcs

我想做一些事情,比如允许用户输入,比如用户搜索"芒果"

请输入水果名称:mango&gt>这是用户输入

Desire Output

Fruit Shape : Oval
Fruit Color : Yellow
Fruit Quantity : 18pcs

到目前为止,这就是我所做的,它只能打印出整行字符串,我在这里做错了什么吗?

echo -n "Please enter the fruit name :"
read fruitName
awk '/'$fruitName'/ {print}'  textFileA

Current Output

mango:oval:yellow:18pcs

推荐答案

你可以这样使用它:

read -p "Please enter the fruit name: " fruitName
awk -F: -v fruitName="$fruitName" '
$1 == fruitName {
   print "Fruit Shape :", $2
   print "Fruit Color :", $3
   print "Fruit Quantity :", $4
}' file

Output:

Please enter the fruit name: mango
Fruit Shape : oval
Fruit Color : yellow
Fruit Quantity : 18pcs

Linux相关问答推荐

空字符串和空文件的区别

用户作用域在Linux内核密钥环(Golang)中是什么意思?

Docker运行错误:exec/app/backend/server:没有这样的文件或目录

X86 程序集 - struct 点 - 存储/返回不正确?

CMake:处理静态库和共享库的正确方法

从父目录中删除目录而不删除父目录 (Linux)

为什么rsp寄存器从0x7FFFFFFFDFD0开始

使用 sed linux 命令和 i sed 命令进行 preprend 时的反向引用

使用 awk 将 csv 拆分为带有标题的多个文件

sig_atomic_t 实际上是如何工作的?

Stripping linux 共享库

如何在 Linux/Unix 上递归复制以abc开头的目录?

仅当当前行中存在字符串时才替换

Linux 非阻塞 fifo(按需日志(log)记录)

优雅地杀死在 Linux 上运行的 .NET Core 守护进程

bash shell 脚本函数定义(如f () {})中使用的括号是什么?它与使用function关键字不同吗?

bash 中的线程?

rename() 是原子的吗?

如何在 linux 中使用 CMake 和 Kdevelop 编译 GLUT + OpenGL 项目?

获取本地时区的 Olson TZ 名称?