How to interchange the places of 1st name and last name?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to interchange the places of 1st name and last name?
Code:
cat emp.lst
12 |Rob Cliff |G.M.
14 |Mark Rob |Chairman
Please use awk to invert the names in the file emp.lst, i.e. the surname should be 1st and then the 1st name. There are trailing spaces even in each field making them fixed length.
I want that the name as shown in the 2nd field should be a bit modified. The 2nd name should come 1st.
For example "Rob Cliff" should become "Cliff Rob"
Write program so that the complete file should be modified in the 2nd field.
i.e., the output should be:
cat emp.lst
12 |Cliff Rob |G.M.
14 |Rob Mark |Chairman
The output is giving the desired result but I am not able to include this output in the complete file. The output of the above only shifts the 2nd name and the 1st name.
My output only gives:
There are trailing spaces even in each field making them fixed length.
Your example text does show trailing spaces, but the two lines are different; the delimiters don't line up. Aren't the fields supposed to have the same length?
Assuming that the output needs to have that same length as the input, then this really requires something more complex than the basic split function. In fact, I'd use gawk's new patsplit instead, or the related FPAT feature.
I've already worked out a solution that works for the above text, but I'm going to put off posting it until you've had a chance to figure it out yourself.
If a recent version of gawk can't be used, then you may be able to first record the length of $2, and then ensure that the output is padded with spaces equal to the total length - text length.
Yes the fields are supposed to be of the same length. The delimiters should line up one below the other. While typing I made it of the same length but after submitting the post it got changed
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.