0

我正在尝试从目录中的多个文件中替换重复的 UUID。即使是同一个文件也可能有重复的 UUID。

我正在使用 Unix 实用程序来解决这个问题。到目前为止,我已经使用 grep、cut、sort 和 uniq 来查找文件夹中所有重复的 UUID 并将其存储在一个文件中(比如 duplicate_uuids)

然后我尝试 sed 通过循环文件来替换 UUID。

filename="$1"
re="*.java"
while read line; do
    uuid=$(uuidgen)
    sed -i'.original' -e "s/$line/$uuid/g" *.java
done < "$filename"

如您所料,我最终用新的 UUID 替换了所有重复的 UUID,但它仍然在整个文件中重复!

有什么 sed 技巧可以为我工作吗?

4

2 回答 2

0

这对我有用:

#!/bin/bash

duplicate_uuid=$1
# store file names in array
find . -name "*.java" > file_names
IFS=$'\n' read -d '' -r -a file_list < file_names

# store file duplicate uuids from file to array
IFS=$'\n' read -d '' -r -a dup_uuids < $duplicate_uuid

# loop through all files
for file in "${file_list[@]}"
do
    echo "$file"
    # Loop through all repeated uuids
    for old_uuid in "${dup_uuids[@]}"
    do
        START=1
        # Get the number of times uuid present in this file
        END=$(grep -c $old_uuid $file)
        if (( $END > 0 )) ; then
            echo "    Replacing $old_uuid"
        fi
        # Loop through them one by one and change the uuid
        for (( c=$START; c<=$END; c++ ))
        do
            uuid=$(uuidgen)
            echo "         [$c of $END] with $uuid"
            sed -i '.original' -e "1,/$old_uuid/s/$old_uuid/$uuid/" $file
        done
    done
    rm $file.original
done
rm file_names
于 2018-11-02T19:17:56.550 回答
0

有很多方法可以做到这一点。如果您想稍后自定义内容,使用函数采用多命令方法可能会给您更大的灵活性,例如:

#!/bin/bash

checkdupes() {
    files="$*"
    for f in $files; do
        filename="$f"
        printf "Searching File: %s\n" "${filename}"
        while read -r line; do
            arr=( $(grep -n "${line}" "${filename}" | awk 'BEGIN { FS = ":" } ; {print $1" "}') )
            for i in "${arr[@]:1}"; do
                sed -i '' ''"${i}"'s/'"${line}"'/'"$(uuidgen)"'/g' "${filename}"
                printf "Replaced UUID [%s] at line %s, first found on line %s\n" "${line}" "${i}" "${arr[0]}"
            done
        done< <( sort "${filename}" | uniq -d )
    done
}

checkdupes /path/to/*.java

因此,这一系列命令的作用是首先对您选择的任何文件中的重复项(如果有)进行排序。它采用这些重复项并使用grepawk创建一个行号数组,找到每个重复项。循环遍历数组(同时跳过第一个值)将允许将重复项替换为新的UUID,然后重新保存文件。

使用重复的列表文件

如果您想使用带有欺骗列表的文件来搜索其他文件并替换UUID每个匹配的文件,只需更改两行:

代替:

for i in "${arr[@]:1}"; do

和:

for i in "${arr[@]}"; do

代替:

done< <( sort "${filename}" | uniq -d )

和:

done< <( cat /path/to/dupes_list )

注意:如果您不想覆盖文件,sed -i ''请在命令开头删除。

于 2018-11-02T06:22:35.513 回答