2つのフィールドを同じ行にまとめる

2つのフィールドを同じ行にまとめる

複数の行があり、各行に2つの異なるフィールドが必要な状況があります。具体的には、参考文献に参考文献リストがありますが、実績年度を知りたいです。

入力例:

Aloise-Young, P.A. (1993). The development of self-presentation.  Self-promotion in 6- to 10-year-old children. Social Cognition, 11, 201-222.
Banerjee, R. (2002). Children's understanding of self-presentational behavior: Links with mental-state reasoning and the attribution of embarrassment. Merril-Palmer Quarterly, 48, 378-404.
Bennett, M., & Wellman, H. (1989). The role of second-order belief-understanding and social context in children's self-attribution of social emotions. Social Development, 9, 126-130.

希望の出力:

Aloise-Young 1993
Banerjee 2002
Bennett 1989

城がわかりますか?cat file | cut -d, -f1

私は年を得ることができますcat file | grep -o "[[:digit:]]\{4\}"

私の問題は、2つの別々の出力がありますが、望む方法で組み合わせる方法がわからないということです。どんなアイデアがありますか?必要なことawkができると思います。

ベストアンサー1

基本的なツールでは、テキスト処理が難しすぎると、アッ

awk -F , '{last_name = $1; sub(/\).*/, ""); sub(/.*\(/, ""); print last_name, $0}'

sedはほぼ同じです。読みにくいですが、awkには逆参照がありません。

sed -n 's/^\([^,]*\),[^(]*(\([^()]*\)).*/\1 \2/p'

この特定のタスクの場合、通常Perlはより簡単です。 non-greedy 反復演算子を使用して、行*?の最初の角かっこ部分をキャプチャすることができます。

perl -l -ne '/^([^,]*),.*?\(([^()]*)\)/ and print "$1 $2"'

おすすめ記事