Displa the names in the file namesAndNumbers last name first, comma separated, and sorted alphabetically

Member_2_4942450
Member_2_4942450 used Ask the Experts™
on
17. Display the names in file namesAndNumbers last name first, comma separated, and sorted alphabetically

Output is below

givens, lisa
jameson, john
perez, leo
williams, dina


File is what is below

john jameson	222-423-1244
dina williams	312-231-2222
lisa givens	141-288-2123
leo perez	312-241-2312

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
ok, so yesterday I gave you an awk command for one of your other questions. Do you remember it?

Here's a hint....

Cat the file, pipe to awk, pipe to sort

the awk command will print the two words in reverse order

sort by default will sort alphabetically.

The awk command I gave you yesterday supressed the newline character. today you'll need it.

for example the command...
awk '{printf("%s\n", $1)}'
will print only the first word of each line. You can print anything you want by just including it in the double quotes where the %s is. For example you could put a comma in there.

Author

Commented:
ok this is what I did

But it gave me just the last name and no comma

I should get

givens, lisa
jameson, john
perez, leo
williams, dina


instead i got
givens
jameson
perez
williams


cat namesAndNumbers | awk '{print $2}' | sort

Open in new window

Commented:
cat namesAndNumbers | awk '{printf("%s, %s\n", $2, $1)
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Commented:
ps, pipe the output to sort.

Author

Commented:
cat namesAndNumbers | awk '{printf("%s, %s\n", $2, $1)

I did that and it gave me
>

I also tried
cat namesAndNumbers | awk '{printf("%s, %s\n", $2, $1) | sort -n

and gave me >
nothing

Commented:
Sorry, my mistake. I'm new on the site and haven't looked into prperly formatting my messages.

The command I gave you is incomplete, It's missing some punctuation. Lets try again...

cat namesAndNumbers | awk '{printf("%s, %s\n", $2, $1)}' | sort

you don't need the -n on the sort since you're doing a alphabetic sort.

Author

Commented:
Ok I need to review or work on  understanding the printF("%s,%s\n", $2, $1

part these is the area I get jacked up on
but it worked
just confused on the meanings of this and the syntex

Commented:
Ok, to make it simple, printf just prints what you tell it.  You give it a series of parameters, the first parameter is always enclosed in double quotes and is the format specifier. The rest of the parameters are the things you want to print.

The simplest printf is just printing a string...

printf("hello world\n")

That will print:

hello world

notice the \n which is a new line character. Here is the same thing again using variables in awk

echo "hello world" | awk '{printf("%s %s\n", $1, $2)}'

The %s is just saying take the value of the variable and display it as a test string. There are specifiers for integers and floating point numbers, for now you don't have to worry about those.

Hope that helps.

Commented:
I'm not used to this forum. I can't edit my posts. I made a typo above, I meant to say "text string" not "test string". lol.

Author

Commented:
Good yet again. You just signed up for this kevinl33 how long ago.

What part of Canada and what do you do for a living?

Author

Commented:
Whats your full name Kevin? You just signed up for this yesterday.

Commented:
I am in Calgary Alberta, a computer programming contractor.

well, I hope you learned a bit tonight, it was cool to see your examples and the code.

Commented:
Yes I just signed up yesterday. Everytime I google a question this site always comes up with the exact topic I'm looking for, but with the answer greyed out. So I thought I'd join and participate. It's a lot more constructive than some of the other sites I've been conversing on.

I don't really want to go public with my full name, we'll just keep it as kevinl33. :-)

Author

Commented:
I did I hope you can help me some more. Your racking up alot of points for only haven signed up yesterday. And you good. What satisfaction do you get when you do these things because its not like your making any money here off this fourm site?

Author

Commented:
What other sites do you use? yes this forum is really good and helps me out allot with all sorts of stuff

Thanks on to my next issue LOL
Got ton's of them LOL

Commented:
No, that's true. I'm not making any money directly. But I am working toward a free membership, so it's sort of like working for money. Plus my name is all over the place, maybe that's worth something as well. I'm planning to write a few articles, we'll see if I can find the time for that. I think it's good for the resume to have some things published out there. Even if it's only on a web forum, better than nothing.

Commented:
oh other sites? haha. mythbusters, trip advisor, plus a few others that will remain nameless. I think I've posted on a handfull of threads only on a couple technical sites, but don't even remember where they are.

Ok, ttyl bud. The wife is calling.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial