尴尬,sed,用于bash:打印“你好” +姓名
我有一个list.csv,其中包含以下内容:
"date";"Mr. Green Tree"
"date";"Mr. Red Apple"
"date";"Mr. Blue Car"
我使用awk + sed获取以下输出:
awk -F ";" '{print $2 }' list.csv | sed 's/"//g'
输出:
Mr Green Tree
Mr Red Apple
Mr Blue Car
现在我想在a 中使用相同的命令for loop 并添加字符串“ hello”
script.sh
get_name () {
name=$(awk -F ";" '{print $2 }' list.csv | sed 's/"//g')
for string in $name; do
echo "Hello" $string
done
}
get_name
输出script.sh:
Hello Mr
Hello Green
Hello Tree
Hello Mr
Hello Red
Hello Apple
Hello Mr
Hello Blue
Hello Car
预期输出:
Hello Mr Green Tree
Hello Mr Red Apple
Hello Mr Blue Car
I have a list.csv with the following content:
"date";"Mr. Green Tree"
"date";"Mr. Red Apple"
"date";"Mr. Blue Car"
I use awk + sed to get the following output:
awk -F ";" '{print $2 }' list.csv | sed 's/"//g'
Output:
Mr Green Tree
Mr Red Apple
Mr Blue Car
Now I want to use the same command in a for loop and add the string "Hello"
script.sh
get_name () {
name=$(awk -F ";" '{print $2 }' list.csv | sed 's/"//g')
for string in $name; do
echo "Hello" $string
done
}
get_name
Output when I execute script.sh:
Hello Mr
Hello Green
Hello Tree
Hello Mr
Hello Red
Hello Apple
Hello Mr
Hello Blue
Hello Car
Expected Output:
Hello Mr Green Tree
Hello Mr Red Apple
Hello Mr Blue Car
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
(使用您的方法...)尝试:
如果您知道输入将为
\ n
终止:来自在这里
或者,如果您无论如何都用bash循环循环,请在bash中进行全部操作:
或 (最建议...) 完全在<代码> awk :
(Using your method...) Try:
If you know the input will be
\n
terminated:Bash loop from here
Or, if you looping over the file with Bash anyway, just do it all in Bash:
Or (most recommended...) entirely in
awk
:一个简单的方法,即使无循环也没有:
输出:
An easy way of doing it without even a for loop:
Output:
它以@dawg描述的方式工作,因此我将其标记为解决方案。
@biffen的建议也在工作
通过bash中的文件内容循环
代码根据他的回答:
It works that way @dawg described, so I mark this as solution.
The suggestion from @Biffen is also working
Looping through the content of a file in Bash
Code based on his answer: