linux - 用于隔离文件中单词的 Bash 脚本

标签 linux bash shell terminal

这是我要提取的初始输入数据:

david ex1=10 ex2=12 quiz1=5 quiz2=9 exam=99
judith ex1=8 ex2=16 quiz1=4 quiz2=10 exam=90
sam ex1=8 quiz1=5 quiz2=11 exam=85
song ex1=8 ex2=20 quiz2=11 exam=87

如何提取每个单词以这种方式进行格式化:

david 
ex1=10
ex2=12
etc...

因为我最终想要这样的输出:

david 12 99
judith 16 90
sam 0 85
song 20 87

当我使用命令运行我的程序时:

./marks ex2 exam < file

最佳答案

假设您的输入文件名为 input.txt,只需使用 trspace char 替换为 new line char命令行工具:

tr ' ' '\n' < input.txt

对于您的第二个请求,您可能必须在每一行中提取特定字段,因此 cutawk 命令可能会有用(请注意,我的示例当然是可以改进的):

while read p; do
  echo -n "$(echo $p | cut -d ' ' -f1) "                  # name
  echo -n "$(echo $p | cut -d ' ' -f3 | cut -d '=' -f2) " # ex2 val
  echo -n $(echo $p | awk -F"exam=" '{ print  $2 }')      # exam val
  echo
done < input.txt

关于linux - 用于隔离文件中单词的 Bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30029135/

相关文章:

python - Jenkins Remote API 是否支持在 View 内运行所有作业?

linux - 如何使用shell按字符替换特殊字符

c++ - Linux 中 XInput* 函数的等价物是什么?

linux - bash 脚本中的命令无法使用可变参数正确执行

bash - 将 bash 中的逗号分隔数据包装成固定的行长度

linux - 使用 "du"命令获取大小和其他信息

regex - 组合多个 sed 命令

Linux 命令 reset(1) 很慢

c++ - 为什么在使用重载的 insertopm (<<) 运算符时会收到 SIGSEGV?

node.js - n( Node 版本管理器)安装后不工作 - "bash: n: command not found"