I have a series of text files with a consistent format like:FirstName: MaryLastName: SmithAddress: 123 Anywhere StCity: NowhereState: TXZip: 77777I need to extract several lines from these files and output them into a csv file in a format like:<filename>,<FirstName>,<City>,<Zip>I can get the fields I want with a simple grep but I don't know how to get the output the way I need it.  Any help would be appreciated.
extract data from text files to csv
text processing
If you only have one record per file then this is a simple read loop.#!/bin/bashread_data(){  local first last addr city state zip  file=$1  while read -r header data  do    case $header in      FirstName:) first=$data ;;       LastName:) last=$data ;;        Address:) addr=$data ;;           City:) city=$data ;;          State:) state=$data ;;            Zip:) zip=$data ;;               *) echo Ignoring bad line $header $data >&2    esac  done < $file  echo $file,$first,$last,$addr,$city,$state,$zip}for file in *srcfiles*do  read_data $filedoneThe read_data function reads each line and splits up the line into a header and a data.  Once we get to the end of the file we just print out the results.We call that function once for each source file via the for loop.Note some potential gotcha's:  If there are commas in the data then this will break things, so you might want to do  echo \$file\,\$first\,\$last\,\$addr\,\$city\,\$state\,\$zip\as the output to enclose everything inside ... layout.  If there's any  in the data then this may cause the CSV to be malformed as well.Adjust the echo line to match the format you want.