如何将java版本结果追加到上面的行中?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (61)

有没有办法我可以将“java版本或权限被拒绝”这一行附加到上面的行中,在它们之间?

/apps/orautil/app/oracle/product/12.1.0/client_1/jdk/jre/lib/
java version "1.6.0_75"

/export/file/node/jre1.8.0_171/lib/
java version "1.8.0_171"

/test/jre1.8.0_171_linux/lib/
java version "1.8.0_171"

/usr/local/tripwire/te/agent/jre/lib/
sh: line 4: /usr/local/tripwire/te/agent/jre/lib/../bin/java: Permission denied

预期成绩

/apps/orautil/app/oracle/product/12.1.0/client_1/jdk/jre/lib/,java version "1.6.0_75" 

/export/file/node/jre1.8.0_171/lib/,java version "1.8.0_171" 

/test/jre1.8.0_171_linux/lib/,java version "1.8.0_171" 

/usr/local/tripwire/te/agent/jre/lib/,sh: line 4: /usr/local/tripwire/te/agent/jre/lib/../bin/java: Permission denied
提问于
用户回答回答于
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

扫码关注云+社区

领取腾讯云代金券