关闭。这个问题需要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/