perl -lanF", " -e "print join(',',map{$1if/(?:[Name|Age|Gender] = )(.+)/}@F) if shift @F eq 'Person1'" persons.txt Lydia,20,F