bash - bash - 在读取时与 grep 结合并剪切
问题描述
我想修改我现有的 bash 脚本。这是它现在的样子:
#! /bin/bash
SAMPLE = myfile.txt
while read SAMPLE
do
name = $SAMPLE
# some other code
done < $SAMPLE
在这种情况下,'myfile'.txt 仅包含一列,其中包含我需要的所有信息。
现在我想修改这个脚本,因为“myfile.txt”现在包含比我需要的更多的列和更多的行。
grep 'TEST' myfile.txt | cut -d "," -f 1
给我我需要的价值观。但是我怎样才能将它集成到我的 bash 脚本中呢?
解决方案
您可以将任何命令的输出通过管道传输到while read
循环中。
试试这个:
#! /bin/bash
INPUT=myfile.txt
grep 'TEST' $INPUT |
cut -d "," -f 1 |
while read SAMPLE
do
name=$SAMPLE
# some other code
done
推荐阅读
- typescript - Typescript:创建一个泛型类型来模拟私有和受保护的测试方法
- c++ - PCL RANSAC 找不到 LINE 模型的解决方案
- cassandra - Cassandra 是否记录作为重试策略一部分的查询尝试?
- python - 从 Python 字典中删除“第一个”项目
- java - 将 controlP5 文本字段中的字符列入黑名单
- python - 应用于数据帧但对 python 模棱两可的简单函数
- css - IIS Wordpress 未在 Windows Server 2012 上加载 Css Js 内容
- angular - 从 Angular 9 中的基类继承服务
- php - 如何在我正在尝试的codeigniter中定义路由但它不起作用
- c# - c# discord 机器人辅助