0

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!

0

1 Answer 1

2

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.
Sign up to request clarification or add additional context in comments.

2 Comments

What would happen if instead of 'M/F' for gender it became numbers? Like years with the company? So you had 13.5, 10.2, 11.8 added? How would that change because I'm assuming you can't search and use $ for both the age and years now? Sorry I'm just trying to figure sed out because I didn't think $ work for more than one case.
$ is just the end of the line. I used it to append text to each line. If you add columns in the middle, that changes nothing on the $ part. If you add columns at the end, you would have to insert years old now in another way, e.g. by replacing a ,.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.