无法将 awk 命令作为 shell 脚本运行
我正在尝试创建一个 shell 脚本来搜索多行 csv 文件中的特定索引。 我正在尝试的代码是:
#!/bin/sh
echo "please enter the line no. to search: "
read line
echo "please enter the index to search at: "
read index
awk -F, 'NR=="$line"{print "$index"}' "$1"
我尝试在 shell 上使用的 awk 命令工作得非常好。但是当我尝试用这个命令创建 shell 脚本时,它失败并且没有输出。它读取行号。和索引。然后根本没有输出。 我做错了什么吗?
我通过键入以下内容在 shell 中运行该文件:
./fetchvalue.sh newfile.csv
i am trying to create a shell script to search for a specific index in a multiline csv file.
the code i am trying is:
#!/bin/sh
echo "please enter the line no. to search: "
read line
echo "please enter the index to search at: "
read index
awk -F, 'NR=="$line"{print "$index"}' "$1"
the awk command I try to use on the shell works absolutely fine. But when I am trying to create a shell script out of this command, it fails and gives no output. It reads the line no. and index. and then no output at all.
is there something I am doing wrong?
I run the file at the shell by typing:
./fetchvalue.sh newfile.csv
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的引用是行不通的。试试这个:
Your quoting is not going to work. Try this:
与其经历引用地狱,不如试试这个:
awk -F, -v line=$line -v myindex=$index 'NR==line {print $myindex}' "$1"
(索引是awk 中的保留字,所以我给它起了一个稍微不同的名称)
Rather than going through quoting hell, try this:
awk -F, -v line=$line -v myindex=$index 'NR==line {print $myindex}' "$1"
(Index is a reserved word in awk, so I gave it a slightly differet name)