簡體   English   中英

Bash:awk的使用和在3種情況下放置到數組中

[英]Bash: awk usage and placement into array with 3 conditions

在過去的幾個月中,我一直依賴於使用以下代碼從文件中獲取所需內容:

declare -a arr_dbs=(`awk -F: -v key='/software/oracle/ora-11' '$2 ~ key{print $1}' /etc/oratab`)

這是通過/ etc / oratab文件運行的,該文件的內容如下:

# This file is used by ORACLE utilities.  It is created by root.sh
# and updated by the Database Configuration Assistant when creating
# a database.

# A colon, ':', is used as the field terminator.  A new line terminates
# the entry.  Lines beginning with a pound sign, '#', are comments.
#
# The first and second fields are the system identifier and home
# directory of the database respectively.  The third filed indicates
# to the dbstart utility that the database should , "Y", or should not,
# "N", be brought up at system boot time.
#
OEM:/software/oracle/agent/agent12c/core/12.1.0.3.0:N
*:/software/oracle/agent/agent11g:N
#dev360:/software/oracle/ora-10.02.00.04.02:Y
#dev364:/software/oracle/ora-10.02.00.04.02:N
dev661:/software/oracle/ora-10.02.00.04.11:Y
dev663:/software/oracle/ora-11.02.00.04.11:Y
dev360:/software/oracle/ora-11.02.00.04.02:Y
dev361:/software/oracle/ora-11.02.00.04.02:Y
dev362:/software/oracle/ora-11.02.00.04.02:N

簡而言之,我正在尋找dev663, dev360, dev361 這是因為他們:

  1. 未注釋掉(行以#開頭)
  2. 包含/software/oracle/ora-11表示一個Oracle 11數據庫(我想可以將其縮短為/ora-11
  3. 該行的末尾有一個:Y 如文檔所述,這意味着它是活動的。

由於這些條件,我遇到了障礙。 以前(上面的代碼)捕獲了所有項目。 我修復了一些問題,所以我認為它將處理:N,但看來我做錯了:

# Collect the databases using a mixture of AWK and regex, and throw it into an array.
declare -a arr_dbs=(`awk -F: -v key='/software/oracle/ora-11' '$2 ~ key{print $1}' /etc/oratab`)

# After initializing our iterator to zero, the loop goes through the array and looks for
# entries that should not be there.
db_counter=0
for i in "${arr_dbs[@]}"
do
    # Grab the last character via AWK. If it's "N" or "n", remove it from the array
    # "N" represents the database no longer being on that server or being active.
    #last_char=`echo $i | awk '{print substr($0,length,1)}'`
    last_char=`echo $i | tail -c 1`
    if [ "$last_char" == "N" ] || [ "$last_char" == "n" ]; then
        #echo $i
        unset arr_dbs[$db_counter]
    fi
    ((db_counter++))
done

滿足這三個條件的任何輔助編寫代碼將不勝感激。 最終目標是要有一個整齊的數組,根據上面的示例,該數組包含

dev663
dev360 
dev361

Shell是一個可以調用工具的環境,而不是用來操縱文本的工具。 操縱文本的工具是awk,您已經在使用它了,因此只需對其進行調整就可以執行所需的操作,而不是試圖迫使shell進行從未設計過的操作:

$ awk -F: -v key='/software/oracle/ora-11' '{sub(/#.*/,"")} ($2~key) && ($3~/Y/){print $1}' file
dev663
dev360
dev361

sub()刪除所有注釋掉的文本(無論每行從何處開始),然后只測試您關心的2個條件。 如果您有任何尾隨空格,我將對Y使用正則表達式比較,而不是對字符串相等性進行比較。

如下所示的awk ,根據Ed Morton的評論進行了更新。

awk -F":" '!/^[[:blank:]]*#/ && /ora-11/ && /:Y[[:space:]]*$/{print $1}' file
dev663
dev360
dev361

我將多個正則表達式組合成一個代表

  • 不是以# (和)開頭
  • 具有模式ora-11 (和)
  • Havin ga模式:Y
 awk -F: '!/^#/ && /ora-11/ && /Y$/ {print $1}' file

dev663
dev360
dev361

sed

$ sed -rn '/ora-11/{s/^([^#].*):.*:Y$/\1/p}' file

dev663
dev360
dev361
$ awk -F: -v key="/software/oracle/ora-11" '$0 ~ "[^#]" key && /Y$/ && $0=$1' file
dev663
dev360
dev361

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM