sed:從一組檔案中刪除路徑列表

sed:從一組檔案中刪除路徑列表

我有一個名為通用.txt包含絕對路徑列表。例如:

/etc
/etc/group
/var/log/syslog

我也有一組文件<主機名稱>.txt它還包含絕對路徑列表。這是一個例子(稱之為主機1.txt):

/root/.bashrc
/var/log/syslog
/etc/hosts
/bin/true
/etc
/sbin/rtmon
/etc/group

我想刪除出現在的每條路徑通用.txt從文件集中的每個文件<檔名>.txt。所以,範例文件主機1.txt上面將變成:

/root/.bashrc
/etc/hosts
/bin/true
/sbin/rtmon

我編寫了以下 Bash 腳本來執行此操作:

#!/bin/bash

set -o nounset
set -o errexit
set -o pipefail

while read -r ONE_PATH
do
   for ONE_FILE in host1.txt host2.txt host3.txt
   do
      sed -i '\:'"$ONE_PATH"':d' "$ONE_FILE"
   done
done < common.txt

我很難正確執行 sed 指令。上面顯示的結果是所有操作的檔案都變空了。

我該如何解決這個問題以實現我的目標?

答案1

#!/bin/bash

set -o nounset
set -o errexit
set -o pipefail

declare -r SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
declare -r FILES_DIR=${SCRIPT_DIR%/*}

while read -r ONE_PATH
do
   for ONE_FILE in $(find "$FILES_DIR" -maxdepth 1 -type f -print)
   do
      if [[ "$ONE_FILE" == *".swp" ]] ||
         [[ "$ONE_FILE" == *"common.txt" ]]; then
         continue
      fi

      sed -i '\|^'"$ONE_PATH"'$|d' "$ONE_FILE"
   done

echo "Done removing $ONE_PATH"
done < "$SCRIPT_DIR"/../common.txt

exit 0

答案2

我可以建議一個不使用 sed 的解決方案嗎?

sort common.txt > common.txt.sorted
for f in host1.txt host2.txt host3.txt ; do
    sort $f > $f.sorted
    diff common.txt.sorted $f.sorted | egrep '^>' | sed -e 's/^> //' > $f.output
    rm $f.sorted
done

種類按字母順序對列表進行排序。 差異尋找 a 文件和 a 文件之間的差異通用.txt埃格雷普挑選出以 開頭的行>,即主機1.txt.已排序但不在common.txt.已排序。最後,sed刪除已新增的前導>(即 > 後面接著一個空格)差異

輸出清單也將按字母順序排列。

答案3

首先,輸入應該反向排序。刪除/etc然後去找/etc/group是沒有用的。然後我們驗證該文件是否可以寫入(如果不能則跳過)。然後 ONE_PATH 應該被轉義,然後 sed 可以完成它的工作,

sort -r common.txt \
| while read -r ONE_PATH
do
   for ONE_FILE in host1.txt host2.txt host3.txt
   do
      if [ -w "$ONE_FILE" ]
      then
        # sed -i '\:'"$ONE_PATH"':d' "$ONE_FILE"
        ONE_PATH_ESC=$(echo "$ONE_PATH" | sed "s!/!\\\/!g")
        sed -i 's/^'"$ONE_PATH_ESC"'//' "$ONE_FILE"
      fi
   done
done

根據提供的測試數據,您將獲得:

$ pr -n host1.txt
1   /root/.bashrc
2
3   /hosts
4   /bin/true
5
6   /sbin/rtmon
7

有3個空白行。

相關內容