列値から二重引用符を削除するシェルスクリプト

列値から二重引用符を削除するシェルスクリプト

10列の入力テキストファイルがあり、ファイルの処理中に中間列にこの種のデータが表示されます。次のように列値が必要です。

列の値を入力: "これは私の新しいプログラムです: "Hello World""

必須列の値:「これは私の新しいプログラムです:Hello World」。

Unix シェルスクリプトやコマンドについて助けてください。お時間をいただきありがとうございます。よろしくお願いします。

ベストアンサー1

すべての二重引用符を削除するには、非常に簡単なオプションは@Daniが提案したようにsedを使用することです。

$ echo "This is my program \"Hello World\"" | sed 's/"//g'

This is my program Hello World

ただし、内側の引用符のみを削除するには、次のように引用符の両方を削除し、先頭と末尾に1つずつ追加することをお勧めします。

次の内容を含むSample.txtファイルがあるとします。

$ cat sample.txt

"This is the "First" Line"
"This is the "Second" Line"
"This is the "Third" Line"

次に、内部引用符のみを削除するには、次のように提案します。

$ cat sample.txt | sed 's/"//g' | sed 's/^/"/' |sed 's/$/"/'

"This is the First Line"
"This is the Second Line"
"This is the Third Line"

説明する:

sed 's/"//g'各行の二重引用符をすべて削除します。

sed's/^/"/'各行の先頭に二重引用符を追加します。

sed 's/$/"/'各行の末尾に二重引用符を追加します。

sed's/|/"|"/g'各パイプの前後に引用を追加します。

お役に立てば幸いです。

編集する:パイプ区切り記号の説明に従ってコマンドを少し変更する必要があります。

Sample.txt を次のように設定します。

$ cat sample.txt

"This is the "First" column"|"This is the "Second" column"|"This is the "Third" column"

その後、パイプラインに代替コマンドを追加すると、最終的なソリューションが提供されます。

$ cat sample.txt | sed 's/"//g' | sed 's/^/"/' |sed 's/$/"/' | sed 's/|/"|"/g'

"This is the First column"|"This is the Second column"|"This is the Third column"

スクリプトオプション

このサンプル.txtファイルを使用してください。

$ cat sample.txt
"This is the "first" column"|12345|"This is the "second" column"|67890|"This is the "third" column"

そしてこのスクリプトは

#!/bin/ksh

counter=1
column="initialized"
result=""
while [[ "$column" != "" ]]
do
    eval "column=$(cat sample.txt | cut -d"|" -f$counter)"
    eval "text=$(cat sample.txt | cut -d"|" -f$counter | grep '"')"
    if [[ "$column" = "$text" && -n "$column" ]]
    then
        if [[ "$result" = "" ]]
        then
            result="_2quotehere_${column}_2quotehere_"
        else
            result="${result}|_2quotehere_${column}_2quotehere_"
        fi
    else
        if [[ -n "$column" ]]
        then
            if [[ "$result" = "" ]]
            then
                result="${column}"
            else
                result="${result}|${column}"
            fi
        fi
    fi
    echo $result | sed 's/_2quotehere_/"/g' > output.txt
    (( counter+=1 ))
done
cat output.txt
exit 0

あなたはこれを得るでしょう:

$ ./process.sh
"This is the first column"|12345|"This is the second column"|67890|"This is the third column"

$ cat output.txt
"This is the first column"|12345|"This is the second column"|67890|"This is the third column"

これがあなたが処理すべきことであることを願っています。

教えてください!

最終編集

スクリプトは、ユーザーが提供した入力行を複数回処理します。唯一の制限は、20列がすべて同じ行にあることです。

#!/bin/ksh

rm output.txt > /dev/null 2>&1
column="initialized"
result=""
lineCounter=1
while read line
do
    print "LINE $lineCounter: $line"
    counter=1
    while [[ ${counter} -le 20 ]]
    do
        eval 'column=$(print ${line} | cut -d"|" -f$counter)'
        eval 'text=$(print ${line} | cut -d"|" -f$counter | grep \")'
        print "LINE ${lineCounter} COLUMN ${counter}: $column"
        if [[ "$column" = "$text" && -n ${column} ]]
        then
            if [[ "$result" = "" ]]
            then
                result="_2quotehere_$(echo ${column} | sed 's/\"//g')_2quotehere_"
            else
                result="${result}|_2quotehere_$( echo ${column} | sed 's/\"//g')_2quotehere_"
            fi
        else
            if [[ "$result" = "" ]]
            then
                result=${column}
            else
                result="${result}|${column}"
            fi
        fi
        (( counter+=1 ))
    done
    (( lineCounter+=1 ))
    echo -e $result | sed 's/_2quotehere_/"/g' >> output.txt
    result=""
done < input.txt
print "OUTPUT CONTENTS:"
cat output.txt

exit 0

ここからは、特定の状況に合わせて動作できるようにする必要があります。

おすすめ記事