有没有一种方法可以将“ java版本或权限被拒绝”行添加到上面的行中,并在它们之间添加??我正在为Red Hat 6的bash上的数据处理目的执行此操作。
谢谢
Changed to Tohono O'odham Nation
预期结果
'
答案 0 :(得分:0)
使用bash
while true; do
IFS= read -r line1
IFS= read -r line2
IFS= read -r line3
status=$?
printf "%s, %s\n%s\n" "$line1" "$line2" "$line3"
(( status != 0 )) && break
done < file
和awk
awk '
/java version/ || /Permission denied/ {printf ", %s\n\n", $0; next}
{printf "%s", $0}
' file
# which can also be written as
awk '{
format = (/java version/ || /Permission denied/) ? ", %s\n\n" : "%s"
printf format, $0
}' file
或者通过操纵输入/输出字段和记录分隔符
awk '
BEGIN {FS="\n"; RS=""; OFS=", "; ORS="\n\n"}
{$1 = $1; print}
' file
# or, more tersely
awk '{$1=$1}1' FS='\n' RS='' OFS=', ' ORS='\n\n' file