Something like this will do the csv part. You'll need a loop around your files and this code assumes one record per file - easy enough to change if that's not true
#!/bin/bash
for F in */latest.txt
do
{
read Name
read Age
read Gender
read Street
read Occupation
} < $F
echo "$Age,$Gender,$Name,$Street,$Occupation" >> outputfile
done
Instead of "27,M,..." that will generate "Age=27,Gender=M,..."
A nitpick: There's nothing bash specific in that code. you can just use #!/bin/sh
Regards,
Alister
---------- Post updated at 03:04 PM ---------- Previous update was at 02:43 PM ----------
A tweaked version of frans' solution which outputs the desired format and can handle multiple records in a file (assuming each line in the file is part of a record, without any blank lines or any data not fitting the 5 line record pattern):
#!/bin/sh
IFS=''
for f in */latest.txt; do
while read -r name; do
read -r age
read -r gender
read -r street
read -r occupation
echo "${age#*=},${gender#*=},${name#*=},${street#*=},${occupation#*=}" >> outputfile
done < "$f"
done
That will never print out the name associated with each record.
Also, splitting on "=" assumes that there will not be any equals signs in the value. It's probably a safe assumption but not necessarly a given (I have had to deal with data which treated the first occurrence of a character specially and any subsequent appearances literally). If there did happen to be another "=" the value of $2 would be truncated relative to the correct value.