0

書式設定されたテキスト内のコンテンツを取得しようとしています。例

ファイルへの入力:

i would like to say ("hi")

i am leaving, ("bye")

who is there? ("crazy cat")

I have a ("dirty dog that needs water")


//

(" ") 内の文字列のみを取得するにはどうすればよいですか。

スペースまたは (" を含む文字列で解析しようとしましたが、スペースを含む文字列を取得することはできません...

現在私が使用している

 cat get_list.txt | tr ' ' '\n'
4

3 に答える 3

1

ルックアラウンド正規表現テクニックを使用してこれを実行してみてください:

$ grep -oP '\("\K[^"]+(?="\))' file.txt
bye
crazy cat
dirty dog that needs water

または、正規表現の手法を引き続き使用するポータブルソリューションを使用します。

perl -lne 'print $& if /\("\K[^"]+(?="\))/' file.txt

または単に:

cut -d'"' -f2 file.txt
于 2013-03-11T21:21:07.840 に答える
1
grep -o -E '\(\".*\"\)' get_list.txt

と を含めたい場合は、それを行う必要があり("ます")

それらが必要ない場合は、次のものが必要です。

sed 's/^.*(\"\(.*\)\").*$/\1/' get_list.txt

説明:

s/       substitute
^.*(\"   all characters from the start of the string until a (" (the " is escaped)
\(.*\)   keep the next bit in a buffer - this is the match I care about
\")      this signals that the bit I'm interested in is over
.*$      then match to the end of the line
/\1/     replace all of that with the bit I was interested in

(注 -パイプは不要であるという有効なコメントに対応して、 grepandコマンドを変更しました)。sed

于 2013-03-11T21:17:06.917 に答える
0

二重引用符で囲まれたテキストのみ(引用符自体は含まない)が必要な場合は、 awkを使用できます。

awk -F\" '{print $2}' get_list.txt
于 2013-03-11T21:21:39.723 に答える