14

ファイルから一重引用符と二重引用符を削除しようとしています。単一の sed コマンドで実行できますか?

やっています :

sed 's/\"//g;s/\'//g' txt file

しかし、このエラーが発生します

`' ' は一致しません。

助けてください。

4

7 に答える 7

22

別の可能性は、次を使用することtrです。

tr -d \'\" file
于 2013-08-16T15:40:46.823 に答える
12

シェルでは、単一引用符のペア内で単一引用符をエスケープすることはできません。ただし、二重引用符のエスケープは許可されています。次の sed コマンドが機能するはずです。

sed "s/['\"]//g" file
于 2013-08-16T13:26:34.613 に答える
2

以下のコマンドを使用できます

sed "s/'/ /g" file.txt > newfile.txt
sed 's/\"//g' newfile.txt > Required_file.txt

Required_file.txt最終出力です。

于 2016-11-22T11:09:30.757 に答える
0

Well, here's what I've came to.

First, I found out with ord() what are codes for single and double quotes characters, and then used $(..) syntax to pass it into unquoted sed expression. I used XX and yy instead of empty strings. Obviously it is faaar from optimal, i.e. they perhaps should be combined into one expression, I encourage you to experiment with it. There are several techniques to avoid quoting problems, you can also put sed expression into separate file, to avoid it to be interpreted by shell. The ord() / chr() trick is also useful when trying to deal with single unreadable characters in output, e.g. UTF strings on non-UTF console.

dtpwmbp:~ pwadas$ echo '"' | perl -pe 'print ord($_) . "\n";'
34
"
dtpwmbp:~ pwadas$ echo "'" | perl -pe 'print ord($_) . "\n";'
39
'
dtpwmbp:~ pwadas$ echo \'\" 
'"
dtpwmbp:~ pwadas$ echo \'\" | sed -e s/$(perl -e 'print chr(34) . "\n"')/XX/g | sed -e s/$(perl -e 'print chr(39) . "\n"')/yy/g 
yyXX
dtpwmbp:~ pwadas$

EDIT (note that this time, both characters are replaced with the same string "yy").There might be some shell utilities for "translating" characters to character codes and opposite, i.e. it should be possible to do this without using perl or other language interpreter.

dtpwmbp:~ pwadas$ echo \'\" | sed -e s/[`perl -e 'print chr(34) . chr(39)'`]/yy/g
yyyy
dtpwmbp:~ pwadas$ 

and here's yet another way in shell, perhaps even simpler

dtpwmbp:~ pwadas$ X="'"; Y='"' ; echo $X$Y; echo $X$Y | sed -e "s/$X/aa/g;s/$Y/bb/g"
'"
aabb
dtpwmbp:~ pwadas$ 
于 2013-08-16T10:55:03.300 に答える