LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-17-2013, 04:08 AM   #1
ravisingh1
Member
 
Registered: Apr 2013
Location: Mumbai
Distribution: Ubuntu13.10
Posts: 291

Rep: Reputation: Disabled
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.
 
Old 04-17-2013, 04:37 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Homework? We are willing to help, but we won't do it for you....

What have you tried this far and what are the problems you run into?
 
Old 04-17-2013, 05:46 AM   #3
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Read the manual.
 
Old 04-17-2013, 09:25 AM   #4
ravisingh1
Member
 
Registered: Apr 2013
Location: Mumbai
Distribution: Ubuntu13.10
Posts: 291

Original Poster
Rep: Reputation: Disabled
Code:
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
I tried this way:
Code:
awk -F '{split($2,arr," "}; print arr[2],arr[1] }' emp.lst
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:
Code:
Cliff Rob 
 Rob Mark
 
Old 04-17-2013, 09:34 AM   #5
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Code:
awk -F '{split($2,arr," "}; print arr[2],arr[1] }' emp.lst
Causes an error message:
Code:
awk: fatal: Unmatched ( or \(: /{split($2,arr," "}; print arr[2],arr[1] }/
You sure that's the code you ran? Cause there's no part of it that splits the words from the pipe (|).
 
Old 04-17-2013, 09:40 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@ravisingh1: Your code doesn't make any sense at all.

Have a look at this:
Code:
$ awk 'BEGIN { FS="[ |]" } { print $5, $4 }' emp.lst
Cliff Rob
Rob Mark
Up to you to figure out how this works.

AWK related links:
 
1 members found this post helpful.
Old 04-18-2013, 08:23 PM   #7
ravisingh1
Member
 
Registered: Apr 2013
Location: Mumbai
Distribution: Ubuntu13.10
Posts: 291

Original Poster
Rep: Reputation: Disabled
It should be

Quote:
awk -F"|" '{split($2,arr," "}; print arr[2],arr[1] }' emp.lst

Last edited by ravisingh1; 04-21-2013 at 03:36 AM.
 
Old 04-21-2013, 11:58 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Quote:
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.

patsplit string function

splitting by content

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.
 
Old 04-22-2013, 03:15 AM   #9
ravisingh1
Member
 
Registered: Apr 2013
Location: Mumbai
Distribution: Ubuntu13.10
Posts: 291

Original Poster
Rep: Reputation: Disabled
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
 
Old 04-22-2013, 09:32 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I believe David has put you on the correct path.
 
Old 04-23-2013, 01:23 AM   #11
ravisingh1
Member
 
Registered: Apr 2013
Location: Mumbai
Distribution: Ubuntu13.10
Posts: 291

Original Poster
Rep: Reputation: Disabled
How come a post in between got vanished!!!!It was from ritu3356
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Linux Ubuntu - Places - Network Places Bookmarks question ejspeiro Linux - Networking 1 04-19-2011 10:50 AM
Interchange Monitors Geminias Linux - Hardware 2 07-21-2006 07:29 AM
Please HELP on Interchange 4.8/Redhat 7.1 webcoder Linux - Newbie 0 11-13-2003 09:52 AM
Interchange gbg Linux - Software 0 09-30-2003 08:52 AM
Anyone here know Interchange? kidwired Linux - Software 2 01-17-2003 12:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration