我需要从包含一个和另一个之间的特定字段的文件中获取记录。示例:
“莫扎特于1月27日 1756 出生于Leopold Mozart(1719-1787)和Anna Maria,néePertl(1720-1778),出生于9 Getreidegasse”< / p>
我只需要获得莫扎特和 1756 之间的字段(1月27日出生)
答案 0 :(得分:1)
这awk
会:
awk -F"Mozart | 1756" '{print $2}' file
was born on 27 January
如果你喜欢所有的话:
grep -oP 'Mozart .*? 1756' file
Mozart was born on 27 January 1756
答案 1 :(得分:0)
因为您专门询问了awk
:
echo "Mozart was born on 27 January 1756 to Leopold Mozart (1719–1787) and Anna Maria, née Pertl (1720–1778), at 9 Getreidegasse" | awk ' match($0, /Mozart(.*)1756/) { print substr($0,RSTART+6,RLENGTH-10) }'
打印
was born on 27 January