尴尬,sed,用于bash:打印“你好” +姓名

发布于 2025-02-11 19:29:39 字数 861 浏览 1 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

岁月无声 2025-02-18 19:29:40

(使用您的方法...)尝试:

awk -F ";" '{print $2 }' list.csv | sed 's/"//g' | while read -r line || [[ -n $line ]]; do 
    printf "Hello %s\n" "$line"
done 

如果您知道输入将为\ n终止:

awk -F ";" '{print $2 }' list.csv | sed 's/"//g' | while read -r line; do 
    printf "Hello %s\n" "$line"
done 

来自在这里


或者,如果您无论如何都用bash循环循环,请在bash中进行全部操作:

while IFS=';' read -r f1 f2; do 
    printf "Hello %s\n" "${f2:1:-1}" 
done <list.csv

(最建议...) 完全在<代码> awk :

awk -F';' '{gsub(/"/,"",$2); print "Hello " $2}' list.csv

(Using your method...) Try:

awk -F ";" '{print $2 }' list.csv | sed 's/"//g' | while read -r line || [[ -n $line ]]; do 
    printf "Hello %s\n" "$line"
done 

If you know the input will be \n terminated:

awk -F ";" '{print $2 }' list.csv | sed 's/"//g' | while read -r line; do 
    printf "Hello %s\n" "$line"
done 

Bash loop from here


Or, if you looping over the file with Bash anyway, just do it all in Bash:

while IFS=';' read -r f1 f2; do 
    printf "Hello %s\n" "${f2:1:-1}" 
done <list.csv

Or (most recommended...) entirely in awk:

awk -F';' '{gsub(/"/,"",$2); print "Hello " $2}' list.csv
樱桃奶球 2025-02-18 19:29:40

一个简单的方法,即使无循环也没有:

cat list.csv | awk -F ";" '{print $2 }' | sed -e 's/\.//g' | sed 's/"//g' | awk '{print "Hello"" "$1,$2,$3}'

输出:

Hello Mr Green Tree
Hello Mr Red Apple
Hello Mr Blue Car

An easy way of doing it without even a for loop:

cat list.csv | awk -F ";" '{print $2 }' | sed -e 's/\.//g' | sed 's/"//g' | awk '{print "Hello"" "$1,$2,$3}'

Output:

Hello Mr Green Tree
Hello Mr Red Apple
Hello Mr Blue Car
夢归不見 2025-02-18 19:29:40

它以@dawg描述的方式工作,因此我将其标记为解决方案。

@biffen的建议也在工作
通过bash中的文件内容循环

代码根据他的回答:

get_name () {
    name=$(awk -F ";" '{print $2 }' list.csv | sed 's/"//g')
    while read p; do
        echo "Hello" $p
    done < $name
}

get_name

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:

get_name () {
    name=$(awk -F ";" '{print $2 }' list.csv | sed 's/"//g')
    while read p; do
        echo "Hello" $p
    done < $name
}

get_name
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文