AIX 中 -A/-B 替換之後/之前的 Grep 行

AIX 中 -A/-B 替換之後/之前的 Grep 行

我使用的是 AIX 6.1,它不支援 in-B-A標誌:

grep: Not a recognized flag: B

假設我想運行:

cat file | grep -E -B4 'Directory entry type.*Indirect' | grep "Database name" | awk  '{print $4}'

在 AIX 中如何實現這種邏輯?

編輯

我的真實程式碼如下:

NAME_EXISTS=`db2 LIST DB DIRECTORY | grep -E -B5 'Directory entry type.*Remote' | grep "Database alias" | awk '{print $4}' | grep -i ${NAME} | wc -l`
if [ ${NAME_EXISTS} -gt 0 ]; then
    db2 LIST DB DIRECTORY | grep -E -A5 "Database alias.*${NAME}"
fi

這個想法是尋找是否存在名為 的遠端資料庫$NAME,如果找到,則顯示開頭的 5 行Database alias.*${NAME}$NAME是獨一無二的Database alias

輸出db2 LIST DB DIRECTORY是這樣的:

 System Database Directory

 Number of entries in the directory = 3

Database 1 entry:

 Database alias                       = OLTPA
 Database name                        = OLTPA
 Local database directory             = /db2/data
 Database release level               = 10.00
 Comment                              =
 Directory entry type                 = Indirect
 Catalog database partition number    = 0
 Alternate server hostname            =
 Alternate server port number         =

Database 2 entry:

 Database alias                       = OLTPF
 Database name                        = OLTP
 Node name                            = OLTPN
 Database release level               = 10.00
 Comment                              =
 Directory entry type                 = Remote
 Catalog database partition number    = -1
 Alternate server hostname            =
 Alternate server port number         =

Database 3 entry:

 Database alias                       = ADMIN
 Database name                        = ADMIN
 Local database directory             = /db2/data
 Database release level               = 10.00
 Comment                              =
 Directory entry type                 = Indirect
 Catalog database partition number    = 0
 Alternate server hostname            =
 Alternate server port number         =

對於NAME=OLTPF輸出將是:

 Database alias                       = OLTPF
 Database name                        = OLTP
 Node name                            = OLTPN
 Database release level               = 10.00
 Comment                              =
 Directory entry type                 = Remote

因為NAME=OLTPE不會有任何輸出。

答案1

ed 可能會提供一種簡單的方法來完成此任務。

如果我們可以假設只有一個匹配項,那麼您的管道的替代方案是使用 ed 並消除不必要的 cat 和輔助 grep:

ed -s file <<\EOED | awk '/Database name/ {print $4}'
    /Directory entry type.*Indirect/-4,//p
    q
EOED

如果有多個,不重疊匹配,可以使用 ed 的全域命令來標記它們:

ed -s file <<\EOED | awk '/Database name/ {print $4}'
    g/Directory entry type.*Indirect/-4,.p
    q
EOED

為了演示重疊匹配的情況,假設我們正在匹配字串foo,並且第 7 行和第 9 行有匹配項,並且我們將每個匹配項的前三行作為上下文,輸出將如下所示:

line 4      <--- context
line 5      <--- context
line 6      <--- context
line 7 foo  <--- matched
line 6      <--- context      <--- repeated
line 7 foo  <--- context      <--- repeated
line 8      <--- context
line 9 foo  <--- matched
line 10
line 11

答案2

我的做法略有不同。首先,運行db2 LIST DB DIRECTORY命令並將其輸出保存到文字檔案中。這樣,您就不需要多次重新運行它。然後,對於每個目標名稱,將名稱傳遞給收集相關行的 awk 腳本:

## Run the db command
tempfile=$(mktemp)
db2 LIST DB DIRECTORY > "$tmpfile"
## I am assuming you will have a loop for the different target names
for name in OLTPF OLTPA; do
  awk -v name="$name" '{
       if(/Database alias/){n=$4; a[n]=$0; i=1}
       if (i<=6 && i>1){ a[n]=a[n]"\n"$0}
       i++;
      }END{if(name in a){print a[name]}}' $tempfile
done

答案3

$ awk -v RS= -F'\n' -v OFS='\n' '$1 ~ " OLTPF$"{for (i=1;i<=6;i++) print $i}' file
 Database alias                       = OLTPF
 Database name                        = OLTP
 Node name                            = OLTPN
 Database release level               = 10.00
 Comment                              =
 Directory entry type                 = Remote

相關內容