[英]Sorting a list in bash 1
i have in test.idx many text and i will all lines with: 我在test.idx中有很多文本,我将用:
# alt: Spanish
# alt: German
# alt: Englisch
my script: 我的脚本:
#!/bin/bash
results=$(cat /home/test.idx | grep "# alt:" | awk '{print $3}')
echo "$results"
output is among themselves: 输出在彼此之间:
Spanish
German
English
how can output one after the other? 如何一个接一个地输出? :
:
Spanish German English
Regards 问候
One way, pipe into paste: 一种方法,将其粘贴到管道中:
awk '{print $3}' | paste -s -
Another, use printf instead of print: 另一个,使用printf而不是print:
awk '{printf "%s ", $3} END { print "" }'
$ awk '/# alt:/{printf "%s%s", (NR>1 ? OFS : ""), $NF} END{print ""}' file
Spanish German Englisch
and in response to your comment under @jas's answer: 并回应您在@jas的回答下的评论:
$ awk -v OFS=', ' '/# alt:/{printf "%s%s", (NR>1 ? OFS : ""), $NF} END{print ""}' file
Spanish, German, Englisch
This only works for one-word languages, if you have to handle "Ancient Greek" for example then you'd need a slightly different solution, eg: 这仅适用于单字语言,例如,如果您必须处理“古希腊语”,那么您需要一个略有不同的解决方案,例如:
$ cat file
# alt: Spanish
# alt: Ancient Greek
# alt: Englisch
# alt: Upper Sorbian
$ awk -v OFS=', ' 'sub(/^# alt: */,""){printf "%s%s", (NR>1 ? OFS : ""), $0} END{print ""}' file
Spanish, Ancient Greek, Englisch, Upper Sorbian
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.