bash 的新手,不知道如何完成它。只能让它为一条线工作,而不会继续进一步的线
New to bash and stumped how to finish this. Only able to make it work for one line and does not continue to further lines
正在尝试从 txt 中的字符串中提取正则表达式并回显状态。
.txt 看起来像这样:
something something something name1 something something Available something something something
something something something name2 something something Available something something something
something something something name3 something something Available something something something
something something something name4 something something No status something something something
something something something name5 something something No status something something something
我能够在第一行找到模式并回显状态
cat status.txt | grep -o $name>/dev/null ; cat status.txt | grep -o "No status">/dev/null && echo $name is offline || echo $name is online
name1 is online
这工作正常;但是我将如何着手在所有线路上进行这项工作,以便我得到
name1 is online
name2 is online
name3 is online
name4 is offline
name5 is offline
我上下看了看,想不通。我也尝试过 sed 和 awk 的变体。没有任何作用。也许我应该为此使用 python 或其他东西。感谢您的帮助!
不要为此使用 grep
。逐行阅读文件,从中提取您需要的信息。
while read -r x y z name rest; do
case "$rest" in
*Available*) printf '%s is online\n' "$name" ;;
*"No status"*) printf '%s is offline\n' "$name ;;
esac
done < status.txt
使用 awk
$ awk '{if ( == "Available") print " is online"; else print " is offline"}' status.txt
name1 is online
name2 is online
name3 is online
name4 is offline
name5 is offline
一个类似的awk
实现使用三进制来控制基于第七个字段的输出可以写成:
awk '{print " is " (=="Available" ? "online" : "offline")}' file
例子Use/Output
使用 file
中的数据,您将:
$ awk '{print " is " (=="Available" ? "online" : "offline")}' file
name1 is online
name2 is online
name3 is online
name4 is offline
name5 is offline
你的方法有问题
您的方法同样存在许多低效问题,特别是当您这样做时:
cat status.txt | grep -o ...; cat status.txt | grep -o ...
您连续犯了两个 UUOc。除非你连接两个(或更多)文件,cat file
是不必要地使用cat
(UUOc) 并且应该避免。相反,只需读取文件或使用重定向。例如:
grep -o ... status.txt; grep -o ... status.txt ...
每个管道 '|'
生成一个单独的子 shell 以将一个进程 (stdout
) 的输出与下一个 (stdin
) 的输入联系起来。没必要cat
到grep
。 grep
可以直接读取文件或通过重定向在 stdin
上读取文件,例如grep ... file
或 grep ... < file
.
正在尝试从 txt 中的字符串中提取正则表达式并回显状态。 .txt 看起来像这样:
something something something name1 something something Available something something something
something something something name2 something something Available something something something
something something something name3 something something Available something something something
something something something name4 something something No status something something something
something something something name5 something something No status something something something
我能够在第一行找到模式并回显状态
cat status.txt | grep -o $name>/dev/null ; cat status.txt | grep -o "No status">/dev/null && echo $name is offline || echo $name is online
name1 is online
这工作正常;但是我将如何着手在所有线路上进行这项工作,以便我得到
name1 is online
name2 is online
name3 is online
name4 is offline
name5 is offline
我上下看了看,想不通。我也尝试过 sed 和 awk 的变体。没有任何作用。也许我应该为此使用 python 或其他东西。感谢您的帮助!
不要为此使用 grep
。逐行阅读文件,从中提取您需要的信息。
while read -r x y z name rest; do
case "$rest" in
*Available*) printf '%s is online\n' "$name" ;;
*"No status"*) printf '%s is offline\n' "$name ;;
esac
done < status.txt
使用 awk
$ awk '{if ( == "Available") print " is online"; else print " is offline"}' status.txt
name1 is online
name2 is online
name3 is online
name4 is offline
name5 is offline
一个类似的awk
实现使用三进制来控制基于第七个字段的输出可以写成:
awk '{print " is " (=="Available" ? "online" : "offline")}' file
例子Use/Output
使用 file
中的数据,您将:
$ awk '{print " is " (=="Available" ? "online" : "offline")}' file
name1 is online
name2 is online
name3 is online
name4 is offline
name5 is offline
你的方法有问题
您的方法同样存在许多低效问题,特别是当您这样做时:
cat status.txt | grep -o ...; cat status.txt | grep -o ...
您连续犯了两个 UUOc。除非你连接两个(或更多)文件,cat file
是不必要地使用cat
(UUOc) 并且应该避免。相反,只需读取文件或使用重定向。例如:
grep -o ... status.txt; grep -o ... status.txt ...
每个管道 '|'
生成一个单独的子 shell 以将一个进程 (stdout
) 的输出与下一个 (stdin
) 的输入联系起来。没必要cat
到grep
。 grep
可以直接读取文件或通过重定向在 stdin
上读取文件,例如grep ... file
或 grep ... < file
.