简体   繁体   English

如何选择特定的行并在bash shell的末尾回显

[英]How to select specific lines and echo at the end in bash shell

How can I take specific lines from pipeline and then echo them at the end. 我如何从管道中提取特定的行,然后在末尾回显它们。 To show what I mean - I have few lines sent through pipeline and proccesing like this 为了说明我的意思-我几乎没有通过管线发送和处理的行

#!bin/bash   
 while read line; do
 echo "$line"
done

I have this input 我有这个输入

foo1 -> long
foo1 -> hell
foo1 -> fail
foo1
fast1 -> fine
fast1 -> good
fast1

I need to do this 我需要这样做

foo1 -> long
foo1 -> hell
foo1 -> fail
fast1 -> fine
fast1 -> good
fast1
foo1

Just an example - there would be much longer input to process 只是一个例子-将需要更长的输入来处理

You can use BASH array to store certain lines in array to be printed later: 您可以使用BASH数组将某些行存储在数组中以便以后打印:

lines=()
while read line; do
 if [[ $someCondition ]]; then
   lines+=("$line")
 else
   echo "$line"
 fi
done

# print array now:
printf "%s\n" "${lines[@]}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM