Skip to content
Advertisement

Parse From Text File into String output

So I have a text file called employees.txt that looks like this…

Billy Madderson, M, 34
Allison McGever, F, 32
Bill Nye, M, 35

I’m trying to write a sed script that will read in the text file then output it in the format “< name > is a < gender >, and is < age > years old now.”

I know I need to set the IFS=, but I’m stuck on how to place the information in the string. Any input is greatly appreciated!

Advertisement

Answer

I know I need to set the IFS=

I don’t see why this would be necessary. As far as I know sed doesn’t use IFS at all.

Replace the first , with is a, M/F by man/woman, the second , with and is and so on. The only tricky part is to only replace the M/F in the second column instead of the first M/F you come across. Luckily, after the second column there are no letters anymore, so that simplifies things.

sed 's/, M/ is a man/;s/, F/ is a woman/;s/, /, and is /;s/$/ years old now./' file

For your example the output is

Billy Madderson is a man, and is 34 years old now.
Allison McGever is a woman, and is 32 years old now.
Bill Nye is a man, and is 35 years old now.
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement