linux - 如何在 bash 中将自己的值添加到此脚本中

标签 linux bash ubuntu console-application script

关闭。这个问题需要details or clarity .它目前不接受答案。












想改进这个问题?通过 editing this post 添加详细信息并澄清问题.


10 天前关闭。







Improve this question




我怎样才能将我自己的值(value)添加到这个脚本中?

echo "Choose column: "
read k
cat file.txt | awk '{print $k}'

最佳答案

您可以将列号作为变量传递给 awk。此外,无需使用 cat因为awk可以读取文件。

#!/bin/bash

echo "Choose column: "
read -r k
awk -v col="${k}" '{print $(col)}' file.txt
示例文件.txt:
col1 col2 col3
1 2 3
4 5 6
7 8 9
输出:
$ ./script 
Choose column: 
1
col1
1
4
7
$ ./script 
Choose column: 
2
col2
2
5
8
$ ./script 
Choose column: 
3
col3
3
6
9

$ ./script 
Choose column: 
4
# Note, no columns printed because there are only 3 columns

关于linux - 如何在 bash 中将自己的值添加到此脚本中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72624964/

相关文章:

linux - unix - 文件中每列的最大(长度)

php - UTF-8贯穿始终

ios -/bin/sh : : No such file or directory Splunk MINT SDK

linux - 奇怪的 "sh"行为

php - 在 Ubuntu 上安装 rdkafka

linux - Sed 命令返回表达式错误

c - 用于在聊天服务器中管理组的数据结构

bash - 用 awk 换行

python - Ubuntu 18.04 上的 KivyMD 安装错误

python - import tensorflow 在 jupyter notebook 中有效,但在命令行中无效。使用 conda 安装 tensorflow 和 jupyter notebook